urllib.request 返回空数据,而 postman 中的相同请求返回正确数据 [英] The urllib.request returns empty data, while the same request in postman returns correct data

查看:44
本文介绍了urllib.request 返回空数据,而 postman 中的相同请求返回正确数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网址:

https://www.grants.gov/grantsws/rest/opportunities/search/

网址负载:

payload = { "startRecordNum":0,
            "sortBy":"openDate|desc",
            "oppStatuses":"forecasted|posted"
          }

网址标题:

headers = {'Accept':'application/json, text/javascript, */*; q=0.01',
                 'Content-Type':'application/json; charset=UTF-8' ,
                 'Origin':'https://www.grants.gov' , 
                 'Accept-Language':'en-US,en;q=0.9,fa-AF;q=0.8,fa;q=0.7,ru;q=0.6' }

我的 Python 代码:

My Python code:

import urllib.request
import urllib.parse

req = urllib.request.Request('https://www.grants.gov/grantsws/rest/opportunities/search/')
req.add_header('Accept','application/json, text/javascript, */*; q=0.01')
req.add_header('Content-Type','application/json; charset=UTF-8')
req.add_header('Origin','https://www.grants.gov')
req.add_header('Accept-Language','en-US,en;q=0.9,fa-AF;q=0.8,fa;q=0.7,ru;q=0.6')
req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36')
payload = {"startRecordNum":0,
                "sortBy":"openDate|desc",
                "oppStatuses":"forecasted|posted"}
data = urllib.parse.urlencode(payload).encode()
# data = data.encode('ascii')


# r = urllib.request.urlopen(req)
with urllib.request.urlopen(req, data) as response:
    print(type(response))
    dataList = json.load(response)
    # searchParams = dataList['searchParams']
    # print(searchParams)
    print(dataList)

我的结果:

{'hitCount': 0, 'startRecord': 0, 'oppHits': [], 'oppStatusOptions': [], 'dateRangeOptions': [], 'suggestion': '', 'eligibilities': [], 'fundingCategories': [], 'fundingInstruments': [], 'agencies': [], 'accessKey': '', 'errorMsgs': []}

虽然我希望上述 dict 键的值不应该为空,因为我使用 Postman 中的 post 请求获得了正确的输出.

Whereas I expect the value for the above dict keys should not be empty as I get the right output using post request in Postman.

我应该怎么做才能获得正确的输出.如果您想探索请求和参数,它是链接...

What should I do in order to get the right output. it is the link if you want to explore the request and params...

在此处输入链接描述

推荐答案

您需要使用 json.dumps(payload) 将有效负载编码为 json 格式:

You need to encode the payload in json format using json.dumps(payload) :

import urllib.request
import json

req = urllib.request.Request('https://www.grants.gov/grantsws/rest/opportunities/search/')
req.add_header('Content-Type','application/json; charset=UTF-8')
payload = {
    "startRecordNum": 0,
    "sortBy":"openDate|desc",
    "oppStatuses":"forecasted|posted"
}
data = json.dumps(payload).encode()

with urllib.request.urlopen(req, data) as response:
    dataList = json.load(response)
    print(dataList)

这篇关于urllib.request 返回空数据,而 postman 中的相同请求返回正确数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆