我試圖決議來自網站的影像鏈接。當我檢查網站上的鏈接時,它是這個:https : //static.nike.com/a/images/c_limit,w_592,f_auto/t_product_v1/df7c2668-f714-4ced-9f8f-1f0024f945a9/chaussure-de- basketball-zoom-freak-3-MZpJZF.png但是當我用我的代碼決議它時,輸出是data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7.
from bs4 import BeautifulSoup
import requests
source = requests.get('https://www.nike.com/fr/w/hommes-chaussures-nik1zy7ok').text
soup = BeautifulSoup(source, 'lxml')
pair = soup.find('div', class_='product-card__body')
image_scr = pair.find('img', class_='css-1fxh5tw product-card__hero-image')['src']
print(image_scr)
我認為代碼不是問題,但我不知道是什么導致鏈接以 base64 格式出現。那么我如何設定代碼以將鏈接呈現為 .png ?
uj5u.com熱心網友回復:
由于要抓取 src 含義的影像資料,因此使用請求從服務器下載資料,您需要使用.content格式如下:
source = requests.get('https://www.nike.com/fr/w/hommes-chaussures-nik1zy7ok').content
uj5u.com熱心網友回復:
發生什么了?
首先,看看你的soup——這是事實。網站提供的并非所有資訊都是靜態的,有很多東西是動態提供的,也是由瀏覽器完成的 -> 所以requests不會以這種方式獲取這些資訊。
解決方法
看看<noscript>您選擇的旁邊,它包含影像的較小版本,并提供src
例子
from bs4 import BeautifulSoup
import requests
source = requests.get('https://www.nike.com/fr/w/hommes-chaussures-nik1zy7ok').content
soup = BeautifulSoup(source, 'lxml')
pair = soup.find('div', class_='product-card__body')
image_scr = pair.select_one('noscript img.css-1fxh5tw.product-card__hero-image')['src']
print(image_scr)
輸出
https://static.nike.com/a/images/c_limit,w_318,f_auto/t_product_v1/df7c2668-f714-4ced-9f8f-1f0024f945a9/chaussure-de-basketball-zoom-freak-3-MZpJZF.png
如果您喜歡“大圖”,只需將引數替換w_318為w_1000...
編輯
關于您的評論 - 有更多解決方案,但仍取決于您喜歡對資訊做什么以及您將使用什么。
以下方法使用selenium不同于requests渲染網站并為您提供“正確的頁面源”,但還需要更多資源requests:
from bs4 import BeautifulSoup
from selenium import webdriver
driver = webdriver.Chrome('C:\Program Files\ChromeDriver\chromedriver.exe')
driver.get('https://www.nike.com/fr/w/hommes-chaussures-nik1zy7ok')
soup=BeautifulSoup(driver.page_source, 'html.parser')
pair = soup.find('div', class_='product-card__body')
image_scr = pair.select_one('img.css-1fxh5tw.product-card__hero-image')['src']
print(image_scr)
輸出
https://static.nike.com/a/images/c_limit,w_592,f_auto/t_product_v1/df7c2668-f714-4ced-9f8f-1f0024f945a9/chaussure-de-basketball-zoom-freak-3-MZpJZF.png
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/346065.html
