gpt4 book ai didi

python - 如何使用请求或其他模块从 url 不变的页面获取数据?

转载 作者:太空宇宙 更新时间:2023-11-04 08:29:27 25 4
gpt4 key购买 nike

我目前正在使用 selenium 转到一个页面:

https://www.nseindia.com/products/content/derivatives/equities/historical_fo.htm

然后选择相关选项并点击Get Data按钮。

然后检索使用 BeautifulSoup 生成的表。

在这种情况下有没有办法使用请求?如果是这样,是否有人可以向我指出教程?

最佳答案

当您选择选项时,您或多或少只是为获取数据按钮设置参数以向其后端发出请求。如果您模仿此 curl 中的请求:

curl 'https://www.nseindia.com/products/dynaContent/common/productsSymbolMapping.jsp?instrumentType=FUTIDX&symbol=NIFTYMID50&expiryDate=31-12-2020&optionType=select&strikePrice=&dateRange=day&fromDate=&toDate=&segmentLink=9&symbolCount=' -H 'Pragma: no-cache' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-US,en;q=0.9' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36' -H 'Accept: */*' -H 'Referer: https://www.nseindia.com/products/content/derivatives/equities/historical_fo.htm' -H 'X-Requested-With: XMLHttpRequest' -H 'Connection: keep-alive' -H 'Cache-Control: no-cache' --compressed

然后你可以在请求中做同样的事情:

import requests

headers = {
'Pragma': 'no-cache',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-US,en;q=0.9',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
'Accept': '*/*',
'Referer': 'https://www.nseindia.com/products/content/derivatives/equities/historical_fo.htm',
'X-Requested-With': 'XMLHttpRequest',
'Connection': 'keep-alive',
'Cache-Control': 'no-cache',
}

params = (
('instrumentType', 'FUTIDX'),
('symbol', 'NIFTYMID50'),
('expiryDate', '31-12-2020'),
('optionType', 'select'),
('strikePrice', ''),
('dateRange', 'day'),
('fromDate', ''),
('toDate', ''),
('segmentLink', '9'),
('symbolCount', ''),
)

response = requests.get('https://www.nseindia.com/products/dynaContent/common/productsSymbolMapping.jsp', headers=headers, params=params)

学习如何做到这一点的好网站:

https://curl.trillworks.com/

关于python - 如何使用请求或其他模块从 url 不变的页面获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54095990/

25 4 0
文章推荐: python - Selenium - 我对服务器的访问量有多大?
文章推荐: html - 按钮元素的边框距实际末端 1 px
文章推荐: javascript - 我在 HTML 中的 TABS 不起作用。它不会将我重定向到
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com