美丽的汤和uTidy
Posted
技术标签:
【中文标题】美丽的汤和uTidy【英文标题】:Beautiful Soup and uTidy 【发布时间】:2010-10-27 13:09:11 【问题描述】:我想将utidy 的结果传递给Beautiful Soup,唉:
page = urllib2.urlopen(url)
options = dict(output_xhtml=1,add_xml_decl=0,indent=1,tidy_mark=0)
cleaned_html = tidy.parseString(page.read(), **options)
soup = BeautifulSoup(cleaned_html)
运行时出现如下错误:
Traceback (most recent call last):
File "soup.py", line 34, in <module>
soup = BeautifulSoup(cleaned_html)
File "/var/lib/python-support/python2.6/BeautifulSoup.py", line 1499, in __init__
BeautifulStoneSoup.__init__(self, *args, **kwargs)
File "/var/lib/python-support/python2.6/BeautifulSoup.py", line 1230, in __init__
self._feed(isHTML=isHTML)
File "/var/lib/python-support/python2.6/BeautifulSoup.py", line 1245, in _feed
smartQuotesTo=self.smartQuotesTo, isHTML=isHTML)
File "/var/lib/python-support/python2.6/BeautifulSoup.py", line 1751, in __init__
self._detectEncoding(markup, isHTML)
File "/var/lib/python-support/python2.6/BeautifulSoup.py", line 1899, in _detectEncoding
xml_encoding_match = re.compile(xml_encoding_re).match(xml_data)
TypeError: expected string or buffer
我收集 utidy 返回一个 XML 文档,而 BeautifulSoup 需要一个字符串。有没有办法投射clean_html?还是我做错了,应该采取不同的方法?
【问题讨论】:
【参考方案1】:将传递给 BeautifulSoup 的值转换为字符串。 在您的情况下,对最后一行进行以下编辑:
soup = BeautifulSoup(str(cleaned_html))
【讨论】:
【参考方案2】:只需将str()
包裹在cleaned_html
周围
将其传递给 BeautifulSoup 时。
【讨论】:
以上是关于美丽的汤和uTidy的主要内容,如果未能解决你的问题,请参考以下文章