我的问题是,我使用 Selenium 在 Roblox 上抓取我的销售额,因为请求每次都会返回错误值,所以我让 selenium 将 JSON 响应写入文本文件。
现在我只想从中获取销售价值,我该怎么做?
Python函数:
def submitData(self):
self.title.setText("Roblox Sales Checker")
try:
ID = self.ID_INPUT.text()
url = f'https://api.roblox.com/Marketplace/ProductInfo?assetId={ID}'
driver.get(url)
driver.add_cookie("My Auth Cookie,but not for you :)")
driver.refresh()
search = driver.find_element_by_css_selector("body > pre").text
f = open('apiResponse.txt', 'w')
f.write(search)
f.close()
f = open('apiResponse.txt', 'r+')
apiResponseText = f.readlines()
print(apiResponseText)
response.txt
:
{"TargetId":6970745869,"ProductType":"User Product","AssetId":6970745869,"ProductId":1183920723,"Name":"beautifulSky","Description":"","AssetTypeId":10,"Creator":{"Id":2657343484,"Name":"Bestgamedev1209","CreatorType":"User","CreatorTargetId":2657343484},"IconImageAssetId":0,"Created":"2021-06-18T15:12:36.253Z","Updated":"2021-06-18T15:12:36.297Z","PriceInRobux":null,"PriceInTickets":null,"Sales":20624,"IsNew":false,"IsForSale":false,"IsPublicDomain":true,"IsLimited":false,"IsLimitedUnique":false,"Remaining":null,"MinimumMembershipLevel":0,"ContentRatingTypeId":0}
如果您
import json
,销售额将如下所示:
json.loads(apiResponseText)["Sales"]
这是一个小例子:
import json
apiResponseText='{"TargetId":6970745869,"ProductType":"User Product","AssetId":6970745869,"ProductId":1183920723,"Name":"beautifulSky","Description":"","AssetTypeId":10,"Creator":{"Id":2657343484,"Name":"Bestgamedev1209","CreatorType":"User","CreatorTargetId":2657343484},"IconImageAssetId":0,"Created":"2021-06-18T15:12:36.253Z","Updated":"2021-06-18T15:12:36.297Z","PriceInRobux":null,"PriceInTickets":null,"Sales":20624,"IsNew":false,"IsForSale":false,"IsPublicDomain":true,"IsLimited":false,"IsLimitedUnique":false,"Remaining":null,"MinimumMembershipLevel":0,"ContentRatingTypeId":0}'
y=json.loads(apiResponseText)["Sales"]
print(y)
输出为:
20624