看来这个信息是通过 JavaScript 加载的:
url = "https://www.courir.com/fr/p/ugg-tasman-1499533.html"
headers = {
"User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N)",
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36"
}
payload = {
"frz-smartcache-fragment": "true",
"frz-timeout": "5000",
"frz-smartcache-v": "2",
"frz-smartcache-placeholders-number": "8"
}
r = requests.get(url, headers=headers, params=payload)
soup = BeautifulSoup(re.search(r"(<.*>)", r.text)
.group().replace("\\", ""), "html.parser")
sizes = {}
for tag in soup.select("[class$='selectable']"):
sizes.setdefault(tag["class"][0], []).append(
tag.select_one("a").get_text().strip("n"))
# 输出结果:
print(sizes)
# {'unselectable': ['40'], 'selectable': ['41', '42', '43', '44', '45', '46']}
如果你只想在尺码为 40
有库存时触发某个操作,可以这样做:
available_sizes = [
tag["title"].split(": ")[-1]
for tag in soup.select(".selectable a")
] # ['41', '42', '43', '44', '45', '46']
if "40" in available_sizes:
...