【问题标题】:Webscraping Roblox网页抓取 Roblox
【发布时间】:2021-09-07 01:30:12
【问题描述】:

我的问题是我正在使用 selenium 对我在 roblox 上的销售进行网络抓取,因为请求每次都会返回错误值,所以我让 selenium 将我的 json 响应写入文本文件。

现在我只想从中获取销售价值,我该怎么做?

这是我的 python 函数,以及我的 response.txt

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)
{"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}

【问题讨论】:

  • 当我将你的 json 粘贴到 json.parser.online.fr 的 json 解析器中时,它失败了。这使得解析变得更加困难。
  • 更改了 json 现在它应该可以工作了

标签: python selenium roblox


【解决方案1】:

如果您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

【讨论】:

  • 打印出来的时候不工作
  • 现在它可以工作了,我不得不将 apiResponse = f.readlines() 更改为 apiResponse = f.read()
  • 所以最后一件事,当我想在我的 gui 中显示值时,它不起作用:/ 我正在使用 PyQt5,这是我要显示它的行:self.output_label.setText("Sales: " + sales)
  • "Sales:" + str(sales) 你不能连接字符串和整数
猜你喜欢
  • 2020-06-18
  • 2017-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-12
  • 2019-03-08
相关资源
最近更新 更多