2009-05-20 2 views
4

Я хочу передать результаты utidy в Beautiful Soup, аля:Beautiful Soup и 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)) 

 Смежные вопросы

  • Нет связанных вопросов^_^