Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python从NSE图表中获取数据?_Python_Web Scraping - Fatal编程技术网

如何使用python从NSE图表中获取数据?

如何使用python从NSE图表中获取数据?,python,web-scraping,Python,Web Scraping,当我移动鼠标时,我试图获取图表上显示的数据。 此图表从URL读取数据 您可以通过请求 此页面需要页眉用户代理,但它甚至可以很短“Mozilla/5.0” import requests import datetime headers = {'User-Agent': 'Mozilla/5.0'} url = 'https://www.nseindia.com/api/chart-databyindex?index=BERGEPAINTEQN' #url = 'https://www.ns

当我移动鼠标时,我试图获取图表上显示的数据。

此图表从URL读取数据

您可以通过
请求

此页面需要页眉
用户代理
,但它甚至可以很短
“Mozilla/5.0”

import requests
import datetime

headers = {'User-Agent': 'Mozilla/5.0'}

url = 'https://www.nseindia.com/api/chart-databyindex?index=BERGEPAINTEQN'
#url = 'https://www.nseindia.com/api/chart-databyindex?index=BERGEPAINTEQN&preopen=true'

r = requests.get(url, headers=headers)

# --- response ---

#print(r.status_code)

data = r.json()

print('name:', data['name'])
print('identifier:', data['identifier'])
print('close price:', data['closePrice'])

prices = data['grapthData'][:10]

for item in prices:
    dt = datetime.datetime.utcfromtimestamp(item[0]/1000)
    value = item[1]
    print(dt, value)
    
结果

name: BERGEPAINT
identifier: BERGEPAINTEQN
close price: 551.45

2020-08-11 09:15:00 553.7
2020-08-11 09:15:01 553.7
2020-08-11 09:15:02 553
2020-08-11 09:15:03 553.95
2020-08-11 09:15:04 553.9
2020-08-11 09:15:05 553.6
2020-08-11 09:15:06 553.85
2020-08-11 09:15:07 553.35
2020-08-11 09:15:08 553.35
2020-08-11 09:15:09 553.35

你的代码在哪里?如果页面使用JavaScript,那么您可能需要Selenium,或者您应该检查哪个url使用JavaScript读取数据并使用它获取数据。您可以添加到此页面的链接。nseindia.com的其他问题示例,但我不知道是否有带图表的页面示例:非常感谢,先生,它运行良好,没有一个错误。