Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Can';t使用请求从网页中刮取一些静态图像链接_Python_Python 3.x_Web Scraping_Python Requests - Fatal编程技术网

Python Can';t使用请求从网页中刮取一些静态图像链接

Python Can';t使用请求从网页中刮取一些静态图像链接,python,python-3.x,web-scraping,python-requests,Python,Python 3.x,Web Scraping,Python Requests,我正试图从一个网站的登录页上抓取图像。所有图像都在搜索结果类名中。当我运行下面的脚本时,没有得到任何结果。我检查了状态\u code,可以注意到脚本正在获得403 由于图像是静态的并且在页面源代码中可用,如何使用请求刮取图像链接 import requests from bs4 import BeautifulSoup url = 'https://pixabay.com/images/search/office/' headers = { 'User-Agent':'Mozill

我正试图从一个网站的登录页上抓取图像。所有图像都在
搜索结果
类名中。当我运行下面的脚本时,没有得到任何结果。我检查了
状态\u code
,可以注意到脚本正在获得
403

由于图像是静态的并且在页面源代码中可用,如何使用请求刮取图像链接

import requests
from bs4 import BeautifulSoup

url = 'https://pixabay.com/images/search/office/'

headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36',
}

r = requests.get(url,headers=headers)
print(r.status_code)
soup = BeautifulSoup(r.text,"lxml")
for item in soup.select(".search_results a > img[src]"):
    print(item.get("src"))

任何与任何浏览器模拟器(如selenium)相关的解决方案都不是我想要的。

此页面使用
JavaScript
Cookies
,这会产生问题。它还检查其他标题,不仅是
用户代理

第一:您必须使用
requests.Session()
来保存cookie。第二:你必须加载一些页面(即主页)才能得到这些cookies。当您拥有cookie时,它将接受其他URL。第三:它还检查其他标头以发送cookie

我在浏览器中运行页面,并在Chrome/Firefox中使用
DevTools
复制real browser使用的所有标题,然后开始使用不同的标题测试请求。最后我发现它需要

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36',
    'Accept-Language': 'en-US;q=0.7,en;q=0.3',
    'Cache-Control': 'no-cache',
}
另一个问题是,当您滚动页面(“延迟加载”)时,页面使用
JavaScript
加载图像,一些url不在
scr
中,而是在
data lazy
中,然后
src
具有
'blank.gif'



看起来Pixabay正在使用Cloudflare的Web应用程序防火墙(WAF)或类似的防火墙。这是相当乏味的手动四处走动

cloudflare scrape
是一个可能会有所帮助的库:

它使用。然而,出于某种原因,它似乎无法在无头模式下找到图像:

from selenium import webdriver
from bs4 import BeautifulSoup


options = webdriver.ChromeOptions()
#options.add_argument("headless")
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(options=options)
try:
    driver.implicitly_wait(3)
    driver.get('https://pixabay.com/images/search/office')
    images = driver.find_elements_by_css_selector('.search_results a > img[src]') # wait for images to show up
    soup = BeautifulSoup(driver.page_source, 'lxml')
    for item in soup.select(".search_results a > img[src]"):
        print(item.get("src"))
finally:
    driver.quit()
印刷品:

https://cdn.pixabay.com/photo/2016/03/09/09/22/workplace-1245776__340.jpg
https://cdn.pixabay.com/photo/2015/01/08/18/26/write-593333__340.jpg
https://cdn.pixabay.com/photo/2015/02/02/11/09/office-620822__340.jpg
https://cdn.pixabay.com/photo/2014/05/02/21/50/home-office-336378__340.jpg
https://cdn.pixabay.com/photo/2016/02/19/11/19/office-1209640__340.jpg
https://cdn.pixabay.com/photo/2015/02/02/11/08/office-620817__340.jpg
https://cdn.pixabay.com/photo/2016/03/26/13/09/cup-of-coffee-1280537__340.jpg
https://cdn.pixabay.com/photo/2017/05/11/11/15/workplace-2303851__340.jpg
https://cdn.pixabay.com/photo/2015/01/09/11/08/startup-594090__340.jpg
https://cdn.pixabay.com/photo/2015/01/08/18/25/startup-593327__340.jpg
https://cdn.pixabay.com/photo/2015/01/08/18/27/startup-593341__340.jpg
https://cdn.pixabay.com/photo/2014/05/02/21/49/home-office-336373__340.jpg
https://cdn.pixabay.com/photo/2015/01/09/11/11/office-594132__340.jpg
https://cdn.pixabay.com/photo/2017/05/04/16/37/meeting-2284501__340.jpg
https://cdn.pixabay.com/photo/2014/05/03/01/03/macbook-336704__340.jpg
https://cdn.pixabay.com/photo/2018/01/11/21/27/desk-3076954__340.jpg
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif

HTTP 403错误表示服务器不想与您通话。也许它认为你是一个机器人(你就是)。非常有用的评论@johngordon。谢谢。还有其他问题-此页面使用JavaScript添加项目(关闭web浏览器中的JavaScript并检查您的url),但
请求
美化组
无法运行
JavaScript
。即使获得状态
200
,也无法获取图像。您可能需要`控制可以运行
JavaScript
的web浏览器,如果您显示
r.text
,那么您会看到包含验证码信息的HTML。还有一条消息是
请打开JavaScript并重新加载页面。
请启用Cookies。
这可以解释问题
403
-它使用JavaScript和
Cookies
检查它是否是真正的web浏览器。感谢@furas提供的宝贵信息。我真的很感激。在我的情况下,我仍然得到403。您是完全运行我的代码还是对自己的代码进行了更改?您是否在所有请求中都使用了
会话
?它将保存cookie并在所有请求中使用它们。您是否已将所有标题添加到会话?这样,所有请求都将使用所有这些头。您可以尝试添加其他标题。在我的例子中,我总是在第一个请求中得到
403
,因为它还没有cookie。但下一个请求已经有cookie,并且它获得状态
200
。但是如果我删除了一些标题,那么它也会得到
403
我完全按照您的建议进行了尝试。这是我得到的结果
403
403
len:0
。谢谢。我还得到了
403403,len:0
。我们都疯了。我检查了你的代码,它对我有效。也许服务器因为某种原因不喜欢你。服务器可能有其他方法来控制用户。如果您在短时间内运行多个请求,那么它可能会将其视为bot并阻止它。或者你的IP被列入黑名单并被阻止。您可以尝试使用一些代理服务器发送每个具有不同IP的请求。但免费的代理服务器通常是无用的——它们不能工作,或者它们可能被列入黑名单和阻止。
https://cdn.pixabay.com/photo/2016/03/09/09/22/workplace-1245776__340.jpg
https://cdn.pixabay.com/photo/2015/01/08/18/26/write-593333__340.jpg
https://cdn.pixabay.com/photo/2015/02/02/11/09/office-620822__340.jpg
https://cdn.pixabay.com/photo/2014/05/02/21/50/home-office-336378__340.jpg
https://cdn.pixabay.com/photo/2016/02/19/11/19/office-1209640__340.jpg
https://cdn.pixabay.com/photo/2015/02/02/11/08/office-620817__340.jpg
https://cdn.pixabay.com/photo/2016/03/26/13/09/cup-of-coffee-1280537__340.jpg
https://cdn.pixabay.com/photo/2017/05/11/11/15/workplace-2303851__340.jpg
https://cdn.pixabay.com/photo/2015/01/09/11/08/startup-594090__340.jpg
https://cdn.pixabay.com/photo/2015/01/08/18/25/startup-593327__340.jpg
https://cdn.pixabay.com/photo/2015/01/08/18/27/startup-593341__340.jpg
https://cdn.pixabay.com/photo/2014/05/02/21/49/home-office-336373__340.jpg
https://cdn.pixabay.com/photo/2015/01/09/11/11/office-594132__340.jpg
https://cdn.pixabay.com/photo/2017/05/04/16/37/meeting-2284501__340.jpg
https://cdn.pixabay.com/photo/2014/05/03/01/03/macbook-336704__340.jpg
https://cdn.pixabay.com/photo/2018/01/11/21/27/desk-3076954__340.jpg
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif
/static/img/blank.gif