[英]How to scrape tables from a site with bs4?
我正在尝试编写一个刮板,它可以在网站上刮取有关产品的信息。
import requests
from bs4 import BeautifulSoup
URL = "https://stockx.com/de-de/air-jordan-1-retro-high-dark-mocha"
headers = {
"user-agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Mobile Safari/537.36"
}
r = requests.get(URL, headers=headers)
soup = BeautifulSoup(r.text, "lxml")
table = soup.find("table", {"class": "latest-sales table table-striped table-condensed "})
rows = table.find_all("tr")
print(rows)
所以现在我试图获取网站上的表格,但我得到一个“AttributeError:'NoneType' object 没有属性'find_all'”。 问题可能是什么?
不要只看表面
你应该学习F12
-> Network
-> F5
-> get ApiUrl
。
这个页面后面有很多ApiUrls,我只找到你需要的,自己研究rest。
这只是一个简单的例子。
import requests
# my country = Tw
# url = 'https://stockx.com/api/products/f7dcb00f-d0a0-4d47-b419-5022b1b84411/activity?state=480¤cy=USD&limit=10&page=1&sort=createdAt&order=DESC&country=TW'
url = 'https://stockx.com/api/products/f7dcb00f-d0a0-4d47-b419-5022b1b84411/activity?state=480¤cy=USD&limit=10&page=1&sort=createdAt&order=DESC&country=??'
res = requests.get(url=url, headers=headers).json()
print(res)
for i in res['ProductActivity']:
print(i)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.