아름다운 수프와 우티 디
-
23-08-2019 - |
문제
결과를 전달하고 싶습니다 우티디 아름다운 수프에게, 알라 :
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
BeautifulSoup은 문자열을 원하고 XML 문서를 반환합니다. cleaned_html 캐스트 방법이 있습니까? 아니면 내가 잘못하고 다른 접근 방식을 취해야합니까?
해결책
그냥 랩 str()
주위에 cleaned_html
BeautifulSoup에 전달할 때.
다른 팁
전달 된 값을 BeautifulSoup으로 변환하십시오. 귀하의 경우 마지막 줄로 다음을 편집하십시오.
soup = BeautifulSoup(str(cleaned_html))
제휴하지 않습니다 StackOverflow