【问题标题】:Scraping data from the <script> tag using python使用 python 从 <script> 标签中抓取数据
【发布时间】:2020-07-20 16:08:04
【问题描述】:

我正在寻找一种将数据从here 抓取到列表的方法。我要提取的数据在

rangeSelector -> 系列 -> 数据

它是特定项目在特定时间的价格的集合。我需要摆脱除数据之外的所有 javascript 代码。然后我将尝试使用这些数据进行绘图和计算。

我是网络抓取的新手,我正在寻找一种简单的一次性解决方案。解决这个问题的最佳方法是什么?

document.addEventListener('DOMContentLoaded', function () {
    var myChart = Highcharts.stockChart('stocks-container', {
        rangeSelector: {
            selected: 1
        },
        yAxis: [{
            labels: {
                align: 'left'
            },
            height: '80%',
            resize: {
                enabled: true
            }
        }, {
            labels: {
                align: 'left'
            },
            top: '80%',
            height: '20%',
            offset: 0
        }],
        plotOptions: {
            column: {
                stacking: 'normal'
            }
        },
        series: [
            {
                name: 'Unit Price (Buy)',
                data: JSON.parse("[[1585902517017,187893.6],[1585906117013,193975.7],[1585909717026,189253.9],[1585913317001,195890.9],[1585916917027,197659.8],[1585920516999,201482.1],[1585924117021,198212.5],[1585927716997,208305.0],[1585929517008,207305.0],[1585933117021,193561.7],[1585936716979,199070.6],[1585938517019,195450.9],[1585942117009,195527.4],[1585945717007,195877.6],

【问题讨论】:

标签: python html web web-scraping


【解决方案1】:

您可以使用re/json 模块解析数据。

例如:

import re
import json
import requests


url = 'https://stonks.gg/products/search?input=Superior%20Fragment'
html_data = requests.get(url).text

d1 = json.loads(re.search(r'Unit Price \(Buy\).*?(\[\[.*?\]\])', html_data, flags=re.S).group(1))
d2 = json.loads(re.search(r'Unit Price \(Sell\).*?(\[\[.*?\]\])', html_data, flags=re.S).group(1))
d3 = json.loads(re.search(r'Instant Buy Volume.*?(\[\[.*?\]\])', html_data, flags=re.S).group(1))
d4 = json.loads(re.search(r'Instant Sell Volume.*?(\[\[.*?\]\])', html_data, flags=re.S).group(1))

print(d1)
print(d2)
print(d3)
print(d4)

打印:

[[1585902517017, 187893.6], [1585906117013, 193975.7], [1585909717026, 189253.9], [1585913317001, 195890.9], [1585916917027, 197659.8], [1585920516999, 201482.1], [1585924117021, 198212.5], [1585927716997, 208305.0], [1585929517008, 207305.0], [1585933117021, 193561.7], [1585936716979, 199070.6], [1585938517019, 195450.9], [1585942117009, 195527.4], [1585945717007, 195877.6], [1585949317016, 198097.6], [1585952917006, 200590.3], [1585956517023, 198363.7], [1585958317074, 193681.3], [1585961917009, 199628.0], [1585967317017, 197546.9], [1585969117024, 195719.5], [1585972716979, 198053.2], [1585974516979, 195370.3], [1585976317029, 194257.0], [1585979917012, 195980.4], [1585981717045, 199915.4], [1585985316979, 199097.0], [1585987117024, 199425.4], [1585990717024, 198317.1], [1585994316979, 207382.3], [1585996117030, 199845.9], [1585999717009, 200711.5], 

...and so on.

【讨论】:

    猜你喜欢
    • 2017-09-20
    • 1970-01-01
    • 1970-01-01
    • 2020-05-19
    • 2016-03-02
    • 1970-01-01
    • 2016-02-03
    • 1970-01-01
    • 2020-12-08
    相关资源
    最近更新 更多