【问题标题】:How to download data behind buttons which call javascript functions?如何下载调用javascript函数的按钮后面的数据?
【发布时间】:2020-07-02 20:07:52
【问题描述】:

我试图从网站下载这些数据。 https://www.nseindia.com/market-data/oi-spurts

如何使用 python 抓取这个?

【问题讨论】:

  • 请尝试使用[Ctrl+F]搜索文档中的'downloadCSV()'函数。也许在一些 标签内,您可能会找到 CSV 的下载链接。

标签: javascript python html web-scraping


【解决方案1】:

JavaScript 函数 downloadCSVgijgo.min.js 的一部分。它调用getCSV,它会遍历表中的字段并动态生成一个 CSV 文件。

幸运的是,您不必处理 CSV 文件或从页面上抓取任何内容。要获取您想要的数据,您所要做的就是向浏览器在访问页面时发出请求的同一个 RESTful API 发出 HTTP GET 请求:

def main():

    import requests

    url = "https://www.nseindia.com/api/live-analysis-oi-spurts-underlyings"

    headers = {
        "user-agent": "Mozilla/5.0"
    }

    response = requests.get(url, headers=headers)
    response.raise_for_status()

    data = response.json()["data"]

    print(f"There are {len(data)} items in total.")

    print(f"The first item is:\n{data[0]}")
    
    return 0


if __name__ == "__main__":
    import sys
    sys.exit(main())

输出:

There are 143 items in total.
The first item is:
{'symbol': 'MOTHERSUMI', 'latestOI': 7182, 'prevOI': 4674, 'changeInOI': 2508, 'avgInOI': 53.66, 'volume': 12519, 'futValue': 53892.6066, 'optValue': 3788085280, 'total': 55585.0344, 'premValue': 1692.4278, 'underlyingValue': 104}
>>> 

【讨论】:

【解决方案2】:

找到某个文件的最终下载链接的一种方法是打开 Web 浏览器的调试器,然后在查看调试器的“网络”选项卡时单击下载链接。

通常你会看到请求页面的javascript调用与url相同,请求的内容等等......

从这里你只需要复制 javascript 发送的请求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-23
    • 1970-01-01
    相关资源
    最近更新 更多