1
start_url=requests.get('http://www.delicious.com/golisoda')
soup=BeautifulSoup(start_url)

此代码显示以下错误:

Traceback (most recent call last):
  File "test2_requests.py", line 10, in <module>
    soup=BeautifulSoup(start_url)
  File "/usr/local/lib/python2.7/dist-packages/bs4/__init__.py", line 169, in __init__
    self.builder.prepare_markup(markup, from_encoding))
  File "/usr/local/lib/python2.7/dist-packages/bs4/builder/_lxml.py", line 68, in prepare_markup
    dammit = UnicodeDammit(markup, try_encodings, is_html=True)
  File "/usr/local/lib/python2.7/dist-packages/bs4/dammit.py", line 203, in __init__
    self._detectEncoding(markup, is_html)
  File "/usr/local/lib/python2.7/dist-packages/bs4/dammit.py", line 373, in _detectEncoding
    xml_encoding_match = xml_encoding_re.match(xml_data)
TypeError: expected string or buffer
4

3 回答 3

6

使用.content响应的 :

start_url = requests.get('http://www.delicious.com/golisoda')
soup = BeautifulSoup(start_url.content)

或者,您可以使用解码后的 unicode 文本:

start_url = requests.get('http://www.delicious.com/golisoda')
soup = BeautifulSoup(start_url.text)

请参阅文档的响应内容部分

于 2012-09-18T14:25:05.427 回答
0
from BeautifulSoup import BeautifulSoup
import urllib2
data=urllib2.urlopen('http://www.delicious.com/golisoda').read()
soup=BeautifulSoup(data)
于 2012-09-18T14:34:47.270 回答
0

您可能需要使用

使用

soup=BeautifulSoup(start_url.read())

或者

soup=BeautifulSoup(start_url.text) 
于 2012-09-18T14:26:57.523 回答