无法使用请求从网页中抓取某些静态图像链接



我正试图从网站的登录页上抓取图像。所有图像都在search_results类名内。当我运行下面的脚本时,我没有得到任何结果。我检查了status_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,都不是我想要的。

这使用Selenium。然而,由于某种原因,它似乎没有在无头模式下找到图像:

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

此页面使用JavaScriptCookies,这会产生问题。它还检查其他标头,而不仅仅是User-Agent

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

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

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'


import requests
from bs4 import BeautifulSoup
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": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
#"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US;q=0.7,en;q=0.3",
"Cache-Control": "no-cache",
#"Connection": "keep-alive",
#"Pragma": "no-cache",
}
s = requests.Session()
s.headers.update(headers)  # it will use there hearders in all requests
# --- get cookies ---
url = 'https://pixabay.com/'
r = s.get(url)
print(r.status_code)  # 403 but it is not problem
# only for test 
#r = s.get(url)
#print(r.status_code)  # 200 because it already have cookies
# --- get images ---
url = 'https://pixabay.com/images/search/office/'
r = s.get(url)
print(r.status_code)
#print(r.text)
results = []
soup = BeautifulSoup(r.text, "lxml")
for item in soup.select(".search_results a > img[src]"):
src = item.get("src")
if src is not None and 'blank.gif' not in src:
print('src:', src)
results.append(src)
else:
src = item.get("data-lazy")
print('data-lazy:', src)
results.append(src)
print('len:', len(results))

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

cloudflare-scrape是一个可能有帮助的库:https://github.com/Anorov/cloudflare-scrape

最新更新