2011-03-08 35 views
0
>>> soup = BeautifulSoup(data) 
    Traceback (most recent call last): 
     File "<stdin>", line 1, in <module> 
     File "/usr/lib/pymodules/python2.6/BeautifulSoup.py", line 1499, in __init__ 
     BeautifulStoneSoup.__init__(self, *args, **kwargs) 
     File "/usr/lib/pymodules/python2.6/BeautifulSoup.py", line 1230, in __init__ 
     self._feed(isHTML=isHTML) 
     File "/usr/lib/pymodules/python2.6/BeautifulSoup.py", line 1263, in _feed 
     self.builder.feed(markup) 
     File "/usr/lib/python2.6/HTMLParser.py", line 108, in feed 
     self.goahead(0) 
     File "/usr/lib/python2.6/HTMLParser.py", line 148, in goahead 
     k = self.parse_starttag(i) 
     File "/usr/lib/python2.6/HTMLParser.py", line 226, in parse_starttag 
     endpos = self.check_for_whole_start_tag(i) 
     File "/usr/lib/python2.6/HTMLParser.py", line 301, in check_for_whole_start_tag 
     self.error("malformed start tag") 
     File "/usr/lib/python2.6/HTMLParser.py", line 115, in error 
     raise HTMLParseError(message, self.getpos()) 
    HTMLParser.HTMLParseError: malformed start tag, at line 5518, column 822 



>>> for each in l[5515:5520]: 
...  print each 
... 
<script> 

    registerImage("original_image", "http://ecx.images-amazon.com/images/I/41h7uHc1jmL._SL500_AA240_.jpg","<a href="+'"'+"http://rads.stackoverflow.com/amzn/click/1592406017"+'"'+" target="+'"'+"AmazonHelp"+'"'+" onclick="+'"'+"return amz_js_PopWin(this.href,'AmazonHelp','width=700,height=600,resizable=1,scrollbars=1,toolbar=0,status=1');"+'"'+" ><img onload="+'"'+"if (typeof uet == 'function') { uet('af'); }"+'"'+" src="+'"'+"http://ecx.images-amazon.com/images/I/41h7uHc1jmL._SL500_AA240_.jpg"+'"'+" id="+'"'+"prodImage"+'"'+" width="+'"'+"240"+'"'+" height="+'"'+"240"+'"'+" border="+'"'+"0"+'"'+" alt="+'"'+"Life, on the Line: A Chef's Story of Chasing Greatness, Facing Death, and Redefining the Way We Eat"+'"'+" onmouseover="+'"'+""+'"'+" /></a>", "<br /><a href="+'"'+"http://rads.stackoverflow.com/amzn/click/1592406017"+'"'+" target="+'"'+"AmazonHelp"+'"'+" onclick="+'"'+"return amz_js_PopWin(this.href,'AmazonHelp','width=700,height=600,resizable=1,scrollbars=1,toolbar=0,status=1');"+'"'+" >See larger image</a>", ""); 
    var ivStrings = new Object(); 
</script> 
>>> 
>>> l[5518-1][822] 
'h' 
>>> 

注:在Ubuntu 10.04美麗的湯畸形的開始標記錯誤

使用Python 2.6.5是不是BeutifulSoup應該忽略script標籤?
不能想出一個辦法擺脫這種:(
什麼建議?

+0

即使刪除所有腳本標籤的方法也可以使用!有沒有運氣與re.sub,後來發現重新不能用於HTML,因爲HTML不是一個正常的lang:X – Nullpoet

回答

2

Pyparsing有一定的HTML標籤的支持,讓更多的強大的腳本不僅僅是直RE的。而且由於它不嘗試解析/處理整個HTML體而只是尋找匹配的字符串表達式,它可以處理格式錯誤的HTML:

html = """<script>  
registerImage("original_image", 
"this is a closing </script> tag in quotes" 
etc.... 
</script> 
""" 

# code to strip <script> tags from an HTML page 
from pyparsing import makeHTMLTags,SkipTo,quotedString 

script,scriptEnd = makeHTMLTags("script") 
scriptBody = script + SkipTo(scriptEnd, ignore=quotedString) + scriptEnd 

descriptedHtml = scriptBody.suppress().transformString(html) 

根據刮什麼樣的HTML的你正在嘗試做的,你也許可以用做整個事情pyparsing。

0

當我在BeautifulSoup中經常打擊腳本標記時,我會將湯對象轉換回字符串,刪除有問題的數據,然後重新獲取數據。適用於您不關心數據的情況。