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