格式错误的开始标记错误 - Python,BeautifulSoup和Sipie - Ubuntu 10.04

nic*_*ius 9 python beautifulsoup

我刚安装了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,因此没有多大意义.关于下一步该怎么做的任何建议?

Dra*_*uan 15

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

如果您使用的是早于2.7.3的Python 2版本,或者早于3.2.2的Python 3版本,则必须安装lxml或html5lib-Python的内置HTML解析器在旧版本中不是很好版本.

我试过这个并且运行良好,就像@Joshua一样

soup = BeautifulSoup(r.text, 'html5lib')
Run Code Online (Sandbox Code Playgroud)


Jos*_*rns 8

您遇到的问题非常常见,它们专门处理格式错误的HTML.在我的例子中,有一个HTML元素,它引用了一个属性的值.我实际上今天碰到了这个问题,所以这样做会发现你的帖子.我最终能够通过html5lib解析HTML来解决这个问题,然后再将它从FineSoup 4中移除.

首先,你需要:

sudo easy_install bs4
sudo apt-get install python-html5lib
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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

  • 我得到`ValueError:Unrecognized treebuilder"beautifulsoup"`(Python 2.7.5,beautifulsoup 4.3.2,html5lib 0.999) (2认同)