开始标记错误-Python、BeautifulSoup和Sipie-Ubuntu 10.04

2024-03-29 06:29:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚刚安装了python、mplayer、beautifulsoup和sipie,在我的Ubuntu 10.04机器上运行Sirius。我跟踪了一些看似简单的文档,但遇到了一些问题。我对Python不太熟悉,所以这可能超出我的能力范围。

我可以安装所有东西,但是运行sipie会得到:

/usr/bin/Sipie/Sipie/Config.py:12: DeprecationWarning: the md5 module is deprecated; use hashlib instead import md5
Traceback (most recent call last): File "/usr/bin/Sipie/sipie.py", line 22, in <module> Sipie.cliPlayer()
File "/usr/bin/Sipie/Sipie/cliPlayer.py", line 74, in cliPlayer completer = Completer(sipie.getStreams())
File "/usr/bin/Sipie/Sipie/Factory.py", line 374, in getStreams streams = self.tryGetStreams()
File "/usr/bin/Sipie/Sipie/Factory.py", line 298, in tryGetStreams soup = BeautifulSoup(data)
File "/usr/local/lib/python2.6/dist-packages/BeautifulSoup-3.1.0.1-py2.6.egg/BeautifulSoup.py", line 1499, in __init__ BeautifulStoneSoup.__init__(self, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/BeautifulSoup-3.1.0.1-py2.6.egg/BeautifulSoup.py", line 1230, in __init__ self._feed(isHTML=isHTML)
File "/usr/local/lib/python2.6/dist-packages/BeautifulSoup-3.1.0.1-py2.6.egg/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 100, column 3

我查看了这些文件和行号,但是由于我对Python不熟悉,所以没有什么意义。下一步怎么办有什么建议吗?


Tags: inpyselfbinlibusrtagfeed
3条回答

BeautifulSoupuses HTMLParser rather than SGMLParser的较新版本(由于SGMLParser已从Python 3.0标准库中删除)。因此,BeautifulSoup无法再正确处理许多格式错误的HTML文档,我相信您在这里会遇到这种情况。

解决您的问题的方法可能是uninstall BeautifulSoup, and install an older version(在Ubuntu 10.04LTS上,Python 2.6仍然适用):

sudo apt-get remove python-beautifulsoup
sudo easy_install -U "BeautifulSoup==3.0.7a"

请注意,这个临时解决方案将不再适用于Python3.0(这可能会成为未来版本的Ubuntu的默认设置)。

您遇到的问题非常常见,它们专门处理格式错误的HTML。在我的例子中,有一个HTML元素用双引号引了一个属性的值。我今天碰到了这个问题,在这个过程中,我看到了你的帖子。我终于能够通过html5lib解析HTML来解决这个问题,然后把它交给BeautifulSoup 4。

首先,你需要:

sudo easy_install bs4
sudo apt-get install python-html5lib

然后,运行以下示例代码:

from bs4 import BeautifulSoup
import html5lib
from html5lib import sanitizer
from html5lib import treebuilders
import urllib

url = 'http://the-url-to-scrape'
fp = urllib.urlopen(url)

# Create an html5lib parser. Not sure if the sanitizer is required.
parser = html5lib.HTMLParser(tree=treebuilders.getTreeBuilder("beautifulsoup"), tokenizer=sanitizer.HTMLSanitizer)
# Load the source file's HTML into html5lib
html5lib_object = parser.parse(file_pointer)
# In theory we shouldn't need to convert this to a string before passing to BS. Didn't work passing directly to BS for me however.
html_string = str(html5lib_object)

# Load the string into BeautifulSoup for parsing.
soup = BeautifulSoup(html_string)

for content in soup.findAll('div'):
    print content

如果您对本代码有任何疑问或需要更具体的指导,请告诉我。:)

假设您使用的是BeautifulSoup4,我在官方文档中发现了一些关于此的信息:http://www.crummy.com/software/BeautifulSoup/bs4/doc/#installing-a-parser

If you’re using a version of Python 2 earlier than 2.7.3, or a version of Python 3 earlier than 3.2.2, it’s essential that you install lxml or html5lib–Python’s built-in HTML parser is just not very good in older versions.

我试过了,效果很好,就像“约书亚”

soup = BeautifulSoup(r.text, 'html5lib')

相关问题 更多 >