【问题标题】:Scrape data from graph generated with Highcharts从使用 Highcharts 生成的图表中抓取数据
【发布时间】:2021-12-28 11:36:56
【问题描述】:

试图从website 中抓取几款游戏的价格历史记录。

Highcharts.js 用于从历史数据中生成包含两个系列的图表。示例页面是https://gg.deals/game/snowrunner/

我可以使用JavaScript 访问数据:

Highcharts.charts[0].series[0].data

Highcharts.charts[0].series[1].data 

但是,我想知道是否有另一种方法可以用来获取数据而无需解析 JavaScript 代码。

【问题讨论】:

  • 嗨@Sam Quinn,如果数据是动态加载的,您也可以尝试拦截请求。但是,直接从图表中获取数据似乎是最好的方法。考虑使用:Highcharts.charts[0].series[1].userOptions.data 以获得更清晰的数据。

标签: python json web-scraping beautifulsoup highcharts


【解决方案1】:

一种选择是使用为 highcharts 提供数据的 api。

注意: 有不同的来源data-without-keyshops-urldata-with-keyshops-url

步骤#1 - 从产品页面生成 dataUrl

soup = bs(requests.get('https://gg.deals/game/snowrunner/').text, 'lxml')

dataUrl = 'https://gg.deals'+soup.select_one('#historical-chart-container')['data-without-keyshops-url']

步骤#2 - 请求 json 数据

r  = requests.get(dataUrl, headers={'X-Requested-With': 'XMLHttpRequest'})
r.json()

示例

import requests,json
from bs4 import BeautifulSoup as bs

url = 'https://gg.deals/game/snowrunner/'
r = requests.get(url)
soup = bs(r.text, 'lxml')

dataUrl = 'https://gg.deals'+soup.select_one('#historical-chart-container')['data-without-keyshops-url']
r  = requests.get(dataUrl, headers={'X-Requested-With': 'XMLHttpRequest'})
r.json()['chartData']['deals']

输出

[{'x': 1581956320000,
  'y': 39.99,
  'shop': 'Epic Games Store',
  'name': '17 Feb 2020 16:18 - 22 May 2020 15:38'},
 {'x': 1590161908000,
  'y': 29.99,
  'shop': 'Epic Games Store',
  'name': '22 May 2020 15:38 - 11 Jun 2020 17:11'},
 {'x': 1591895501000,
  'y': 39.99,
  'shop': 'Epic Games Store',
  'name': '11 Jun 2020 17:11 - 16 Jun 2020 13:18'},
 {'x': 1592313517000,
  'y': 31.99,
  'shop': 'Epic Games Store',
  'name': '16 Jun 2020 13:18 - 30 Jun 2020 13:20'},
 {'x': 1593523255000,
  'y': 39.99,
  'shop': 'Epic Games Store',
  'name': '30 Jun 2020 13:20 - 23 Jul 2020 16:08'},
 {'x': 1595520520000,
  'y': 31.99,
  'shop': 'Epic Games Store',
  'name': '23 Jul 2020 16:08 - 6 Aug 2020 15:03'},
 {'x': 1596726196000,
  'y': 39.99,
  'shop': 'Epic Games Store',
  'name': '6 Aug 2020 15:03 - 24 Sep 2020 16:19'},...]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    相关资源
    最近更新 更多