試圖從網站上抓取幾款游戲的價格歷史記錄。
Highcharts.js 用于根據歷史資料生成具有兩個系列的圖表。示例頁面是https://gg.deals/game/snowrunner/。
我可以使用以下方法訪問資料JavaScript:
Highcharts.charts[0].series[0].data
和
Highcharts.charts[0].series[1].data
但是,我想知道是否有另一種方法可以用來獲取資料而無需決議JavaScript代碼。
uj5u.com熱心網友回復:
一種選擇是使用為 highcharts 提供資料的 api。
注意: 有不同的來源data-without-keyshops-url和data-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'},...]
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/395912.html
下一篇:從JSON中獲取特定資料
