首页 > 解决方案 > 为什么 request.get() 在 python 中给我一个错误?

问题描述

import requests
url="https://beta.nseindia.com/api/option-chain-indices?symbol=NIFTY"
requests.get(url)

我正在使用请求,但它给了我一个超时错误..但是当我在浏览器中打开相同的链接时它工作正常

标签: pythonpython-requests

解决方案


尝试使用一些用户代理字符串标头:

import requests
url="https://beta.nseindia.com/api/option-chain-indices?symbol=NIFTY"
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.get(url, headers=headers)


>>> response
<Response [200]>
>>>

beta.nseindia.com/有一个阻止来自机器人的请求的策略。


推荐阅读