首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取按钮内部的数据。BS4 +请求

获取按钮内部的数据。BS4 +请求
EN

Stack Overflow用户
提问于 2020-12-20 09:07:28
回答 1查看 33关注 0票数 0

<button class="add-to-cart btn btn-primary" data-buttontext="Add to Cart" data-gtmdata='{"productInfo":{"sku":"225169","productID":"11108140","name":"PlayStation 5","category":"Video Games/PlayStation 5/Consoles","brand":"Sony","subGenre":"","platform":"PlayStation 5","condition":"New","variant":"New","genre":"","availability":"Not Available","productType":"regular","zoneSource":"PDP"},"price":{"sellingPrice":"499.99","basePrice":"499.99","currency":"USD"}}' data-pid="11108140" disabled=""> </button>

尝试从这里获取“可用性”:“不可用”。

代码语言:javascript
复制
import requests
import re
from bs4 import BeautifulSoup
header = {"User-Agent":"Mozilla/5.0"}
GameStopItem = 'https://www.gamestop.com/video-games/playstation-5/consoles/products/playstation-5/11108140.html'
r = requests.get(GameStopItem, headers=header)
r = BeautifulSoup(r.content, 'html.parser')
GameStopStock = r.find(class_="add-to-cart btn btn-primary")
print(GameStopStock)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-20 09:26:10

我得到了你的代码的403 Forbidden状态。也许你可以访问,而我不能。如果是这样的话,你可以使用如下代码:

代码语言:javascript
复制
# Similate your content as a soup
content = """<button class="add-to-cart btn btn-primary" data-buttontext="Add to Cart" data-gtmdata='{"productInfo":{"sku":"225169","productID":"11108140","name":"PlayStation 5","category":"Video Games/PlayStation 5/Consoles","brand":"Sony","subGenre":"","platform":"PlayStation 5","condition":"New","variant":"New","genre":"","availability":"Not Available","productType":"regular","zoneSource":"PDP"},"price":{"sellingPrice":"499.99","basePrice":"499.99","currency":"USD"}}' data-pid="11108140" disabled=""> </button>"""
soup = BeautifulSoup(content)

# Get data from button as a dictionary:
product = eval(soup.find('button', class_='add-to-cart btn btn-primary').get('data-gtmdata'))

# And retrieve availability
product['productInfo']['availability']
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65376064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档