我正在尝试在Python上做一个简单的网络抓取机器人,以便在我女朋友最喜欢的运动鞋上市时向她展示,我看过很多视频并尝试了很多东西,关于运动鞋的标题和其他信息正在工作,但结果总是
None
,我也在其他网站上尝试过并得到相同的结果。
import requests
from bs4 import BeautifulSoup
url = "https://www.vans.com.br/tenis-ultrarange-rapidweld-black-white/p/1003500430051U?gad_source=1"
headers = {'User-Agent': "insera aqui seu User Agent"} ## retirei meu user agent mas só pra nao postar com ele
site = requests.get(url, headers=headers)
soup = BeautifulSoup(site.content, 'html.parser')
title = soup.find('h1', class_= 'product-name').get_text()
price = soup.find('div', class_='ta-product-price__total' or 'product-price')
print(title)
print(price)
我尝试过在可变价格中使用许多不同的东西获取价格,将
div
切换到span
等东西,但没有成功,也在其他网站上尝试了同样的事情并得到相同的结果。
您看到的数据是通过 JavaScript 从外部 URL 加载的。你可以尝试一下:
import requests
number = "1003500430051U"
url = f"https://www.vans.com.br/arezzocoocc/v2/vans/products/{number}/dynamic-product-fields?fields=DYNAMIC_FIELDS_PDP"
headers = {
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0"
}
data = requests.get(url, headers=headers).json()
for c in data["colorOptions"]:
if c["code"] == number:
print(c["name"], data["price"]["value"])
break
打印:
Tênis Ultrarange Rapidweld Black White 549.99