簡體   English   中英

Python 請求 GET 未獲得 JSON 有效負載?

[英]Python requests GET not getting the JSON payload?

我正在嘗試從以下 URL 中獲取 JSON 數據:

import requests as r

url = "https://www.nseindia.com/json/CorporateFiling/CF-corpactions-equity.json"

header = {
        'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:65.0) Gecko/20100101 Firefox/65.0',
        "X-Requested-With": "XMLHttpRequest"
    }
resp = r.get(url, stream=True, timeout=20, headers=header)
j = resp.json()

我通過這樣做得到 JSON ,但在檢查中我看到數據在Response payload部分中,而不是在j中。

我以前從未遇到過這個問題,我的搜索引導我POST問題。

我使用postman對其進行了測試

User-Agent價值是你的問題

你可以簡單地刪除它,它會工作

郵遞員-測試-img

我可能錯了,沒有正確回答問題,但是從 UI 獲取的數據和從 API 獲取的數據是相同的:

import json
import requests
from selenium import webdriver

url = 'https://www.nseindia.com/json/CorporateFiling/CF-corpactions-equity.json'
header = {
        'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:65.0) Gecko/20100101 Firefox/65.0',
        "X-Requested-With": "XMLHttpRequest"
    }
driver = webdriver.Chrome()
driver.get(url)
content = driver.find_element_by_xpath('//pre').text
driver.quit()
response = requests.get(url,
                        stream=True,
                        timeout=20,
                        headers=header
                        )
print(json.loads(content) == response.json())
assert json.loads(content) == response.json() 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM