如何在调用javascript函数的按钮后面下载数据?

2024-06-28 11:16:54 发布

您现在位置:Python中文网/ 问答频道 /正文

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

enter image description here

如何使用python来解决这个问题


Tags: 数据httpscomdata网站wwwmarketoi
2条回答

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}
>>> 

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

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

从这里,您只需要复制javascript发送的请求

相关问题 更多 >