Frage

Ich möchte die Ergebnisse übergeben utidy Schöne Suppe, ala:

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)

Wenn er gestartet wird, werden die folgenden Fehler auf:

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

Ich sammle utidy gibt ein XML-Dokument während BeautifulSoup einen String will. Gibt es eine Möglichkeit cleaned_html zu werfen? Oder bin ich es falsch zu machen und sollte einen anderen Ansatz?

War es hilfreich?

Lösung

Just wickeln str() um cleaned_html  beim Passieren es BeautifulSoup.

Andere Tipps

Konvertieren Sie den Wert auf BeautifulSoup in einen String übergeben. In Ihrem Fall, gehen Sie wie folgt bearbeiten auf die letzte Zeile:

soup = BeautifulSoup(str(cleaned_html))
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top