beautiful soup malformed start tag error
>>> 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
>>&开发者_如何转开发gt; 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://www.amazon.com/gp/product/images/1592406017/ref=dp_image_0?ie=UTF8&n=283155&s=books"+'"'+" 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://www.amazon.com/gp/product/images/1592406017/ref=dp_image_text_0?ie=UTF8&n=283155&s=books"+'"'+" 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'
>>>
Note : using Python 2.6.5 on ubuntu 10.04
Isn't BeutifulSoup supposed to ignore script tags ?
Cant figure out a way out of this :( any suggestions ??Pyparsing has some HTML tag support that makes for more robust scripts than just straight RE's. And since it doesn't try to parse/process the entire HTML body but instead just looks for matching string expressions, it can handle badly formed 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)
Depending on what kind of HTML scraping you are trying to do, you might be able to do the whole thing using pyparsing.
When I hit script tags in BeautifulSoup often times I will convert the soup object back to a string, remove the offending data and then re-Soup the data. Works when you dont care about the data.
精彩评论