lazymf
lazymf

Reputation: 355

<script> tag and HTMLParseError

I'm trying to parse html with BeautifulSoup and got strange error. Here's the minimal code which reproduces the problem. (Windows 7 32-bit, ActivePython 2.7).

from bs4 import BeautifulSoup
s = """
<html>
<script>
var pstr = "<li><font color='blue'>1</font></li>";
for(var lc=0;lc<o.length;lc++){}
</script>
</html>
"""
p = BeautifulSoup(s)

Traceback:

Traceback (most recent call last):
  File "<pyshell#69>", line 1, in <module>
    p = BeautifulSoup(s)
  File "C:\Python27\lib\site-packages\bs4\__init__.py", line 168, in __init__
    self._feed()
  File "C:\Python27\lib\site-packages\bs4\__init__.py", line 181, in _feed
    self.builder.feed(self.markup)
  File "C:\Python27\lib\site-packages\bs4\builder\_htmlparser.py", line 56, in feed
    super(HTMLParserTreeBuilder, self).feed(markup)
  File "C:\Python27\lib\HTMLParser.py", line 108, in feed
    self.goahead(0)
  File "C:\Python27\lib\HTMLParser.py", line 148, in goahead
    k = self.parse_starttag(i)
  File "C:\Python27\lib\HTMLParser.py", line 229, in parse_starttag
    endpos = self.check_for_whole_start_tag(i)
  File "C:\Python27\lib\HTMLParser.py", line 304, in check_for_whole_start_tag
    self.error("malformed start tag")
  File "C:\Python27\lib\HTMLParser.py", line 115, in error
    raise HTMLParseError(message, self.getpos())
HTMLParseError: malformed start tag, at line 5, column 25

If you remove the line starting with 'var pstr = ...', parse will work perfectly. Is there a way to get the correct parse of html code like this?

Upvotes: 1

Views: 914

Answers (1)

daedalus
daedalus

Reputation: 10923

You may try the older version of BS or install a different parser. See the documentation on "you need a parser" and "installing a parser" on the BeautifulSoup website.

Your current code works on Python 2.7 and BS3:

from BeautifulSoup import BeautifulSoup
s = """
<html>
<script>
var pstr = "<li><font color='blue'>1</font></li>";
for(var lc=0;lc<o.length;lc++){}
</script>
</html>
"""
p = BeautifulSoup(s)

print p.find('script').text

and produces this output:

var pstr = "<li><font color='blue'>1</font></li>";
for(var lc=0;lc<o.length>

Upvotes: 1

Related Questions