如何使用python获得json响应?

2024-05-16 09:36:42 发布

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

我想从公共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")

但在这两种方法中,我都得到了超时错误


Tags: httpsselfcomjsonurlresponse错误requests
1条回答
网友
1楼 · 发布于 2024-05-16 09:36:42

您必须添加用户代理标头

导入请求

try:
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'}
    r = requests.get("https://www1.nseindia.com/homepage/Indices1.json", headers=headers)
    print(r.json())
except Exception as e:
    print(e)
    print("Timeout occurred")

相关问题 更多 >