2009-05-20 18 views
4

は、私は美しいスープにutidyの結果を渡したい、ALA:美しいスープとuTidy

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は文字列を必要とします。 cleaned_htmlをキャストする方法はありますか?あるいは私はそれを間違ってやっていますか?別のアプローチを取るべきですか?

答えて

11

str()周りにcleaned_html を渡してBeautifulSoupに渡します。

2

BeautifulSoupに渡された値を文字列に変換します。 あなたの場合は、最後の行に次の編集を行います。

soup = BeautifulSoup(str(cleaned_html))