【问题标题】:Scraping the average daily sales from a graph从图表中提取平均每日销售额
【发布时间】:2021-07-23 19:14:15
【问题描述】:

平均每日销售额是图表的一部分,这就是我努力抓取数据的原因。如果有办法从图表中抓取数据(主要是每天的销售额),我们将不胜感激。

 link='https://waxpeer.com/karambit-gamma-doppler-factory-new/item/21733106690'

我的大部分代码都使用 selenium,但我不介意使用其他方法。

time.sleep(20)
linkos = driver.find_element_by_xpath('//* 
[@id="main_layout"]/section/div/div[2]/div[1]/h3')
for sales in linkos:
print(sales)

xpath 只包含看起来像 'd=M0,0 L6.965,0 a0,0 0 0 1 0,0 L6.965,42.6 a0,0 0 0 1 -0,0 L0,42.6 a0 的数据,0 0 0 1 -0,-0 L0,0 a0,0 0 0 1 0,-0 Z'

我是否必须找出每天的特定值的数据是什么,然后找到平均值?

如果您需要查看我可以发送但我不想拥挤发布的其余代码。

【问题讨论】:

  • 这是可能的,但对于仅 Selenium 的解决方案来说确实非常复杂。我想这可以用漂亮的汤来完成
  • 发布您的代码...
  • 我不介意使用 bs4,我将发布当前代码以及一些额外信息

标签: python html selenium web-scraping bots


【解决方案1】:

该数据来自您可以模仿的 API POST 请求

import requests

headers = {'content-type': 'application/json;charset=UTF-8'}
data = '{"name":"\u2605 Karambit | Gamma Doppler (Factory New)"}'
r = requests.post('https://waxpeer.com/api/get-sales-history', headers=headers, data=data.encode('utf-8'))
r.json()

你也可以写成:

import requests

headers = {'content-type': 'application/json;charset=UTF-8'}
data = {"name":"★ Karambit | Gamma Doppler (Factory New)"}
r = requests.post('https://waxpeer.com/api/get-sales-history', headers=headers, json=data)
r.json()

不确定您是否打算对返回的平均值进行平均,例如

import requests
from numpy import mean

headers = {'content-type': 'application/json;charset=UTF-8'}
data = {"name":"★ Karambit | Gamma Doppler (Factory New)"}
r = requests.post('https://waxpeer.com/api/get-sales-history', headers=headers, json=data)
history = r.json()
print(f"For data in data range: {' - '.join([history['data'][i]['day'].split('T')[0] for i in range(0, -2, -1)])}" \
       " mean was {:,.2f}".format(mean([i['avg'] for i in history['data']])))

【讨论】:

  • say w 是为几条数据实现此代码我可以用变量替换“★ Karambit | Gamma Doppler (Factory New)”吗?
  • 听起来很可能。试一试看看。
  • 它确实有效,感谢您的帮助。我会做 print(r.json())['count'] 来得到计数吗?
  • [i['count'] for i in history['data']] 用于个体计数和天数/数据点len(history['data'])
  • 你是一个绝对的传奇。我可以给你买一品脱或咖啡什么的吗?你真的让我很开心。
猜你喜欢
  • 1970-01-01
  • 2017-06-10
  • 1970-01-01
  • 1970-01-01
  • 2016-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多