我想从公共url获取一个打印json响应。它给我超时错误,但当我使用浏览器或postman时,我能够看到响应
url:https://www1.nseindia.com/homepage/Indices1.json
使用scrapy
:
class StockSpider(scrapy.Spider):
name = "pe"
urls = [
"https://www1.nseindia.com/homepage/Indices1.json"
]
def start_requests(self):
for url in self.urls:
yield scrapy.Request(url=url, callback=self.parse)
def parse(self, response):
json_response = json.loads(response.body_as_unicode())
print("+++++++++++++++++++++ printing json response +++++++++++++++++++")
print(json_response)
使用requests
:
try:
r = requests.get("https://www1.nseindia.com/homepage/Indices1.json")
print(r.json())
except:
print("Timeout occurred")
但在这两种方法中,我都得到了超时错误
您必须添加用户代理标头
导入请求
相关问题 更多 >
编程相关推荐