简体   繁体   English

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

[英]cannot run BeautifulSoup using requests.get(url)

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

this code is displaying the following error:此代码显示以下错误:

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

Use the .content of the response:使用响应的.content

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

Alternatively, you can use the decoded unicode text:或者,您可以使用解码的 unicode 文本:

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

See the Response content section of the documentation.请参阅文档的响应内容部分

you probebly need to Use你可能需要使用

using使用

soup=BeautifulSoup(start_url.read())

or要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM