首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >出现错误的BeautifulSoup响应

出现错误的BeautifulSoup响应
EN

Stack Overflow用户
提问于 2014-07-16 05:54:17
回答 2查看 17.2K关注 0票数 11

我正试着用废话把我的脚弄湿。我试着用自己的方式浏览文档,但在第一步我就遇到了一个问题。

这是我的代码:

代码语言:javascript
复制
from bs4 import BeautifulSoup
soup = BeautifulSoup('https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=5....1b&per_page=250&accuracy=1&has_geo=1&extras=geo,tags,views,description')

print(soup.prettify())

这是我得到的回应:

代码语言:javascript
复制
Warning (from warnings module):
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/bs4/__init__.py", line 189
'"%s" looks like a URL. Beautiful Soup is not an HTTP client. You should probably use an     
HTTP client to get the document behind the URL, and feed that document to Beautiful Soup.' % markup)
UserWarning: "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=5...b&per_page=250&accuracy=1&has_geo=1&extras=geo,tags,views,description" 
looks like a URL. Beautiful Soup is not an HTTP client. You should 
probably use an HTTP client to get the document behind the URL, and feed that document    
to Beautiful Soup.
https://api.flickr.com/services/rest/?method=flickr.photos.search&api;_key=5...b&per;_page=250&accuracy;=1&has;_geo=1&extras;=geo,tags,views,description

是因为我试图调用http**s**,还是另一个问题?谢谢你的帮忙!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-07-16 05:58:55

您正在将URL作为字符串传递。相反,您需要通过urllib2requests获取页面源代码

代码语言:javascript
复制
from urllib2 import urlopen  # for Python 3: from urllib.request import urlopen
from bs4 import BeautifulSoup

URL = 'https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=5....1b&per_page=250&accuracy=1&has_geo=1&extras=geo,tags,views,description'
soup = BeautifulSoup(urlopen(URL))

注意,你不需要对urlopen()的结果调用read()BeautifulSoup允许第一个参数是一个类似文件的对象,urlopen()返回一个类似文件的对象。

票数 19
EN

Stack Overflow用户

发布于 2014-07-16 05:59:08

这个错误说明了一切,您正在向Beautiful Soup传递一个URL。您需要首先获取网站内容,然后才将内容传递给BS。

要下载内容,可以使用urlib2

代码语言:javascript
复制
import urllib2
response = urllib2.urlopen('http://www.example.com/')
html = response.read()

以及以后的

代码语言:javascript
复制
soup = BeautifulSoup(html)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24768858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档