无法使用requests.get(url)运行BeautifulSoup

时间:2012-09-18 14:23:22

标签: python beautifulsoup python-requests

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

3 个答案:

答案 0 :(得分: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)

请参阅文档的Response content section

答案 1 :(得分:0)

您可能需要使用

使用

soup=BeautifulSoup(start_url.read())

soup=BeautifulSoup(start_url.text) 

答案 2 :(得分:0)

from BeautifulSoup import BeautifulSoup
import urllib2
data=urllib2.urlopen('http://www.delicious.com/golisoda').read()
soup=BeautifulSoup(data)