本文于2026-03-28更新。 如发现问题或者有建议,欢迎提交 Issue
这一次接口函数的学习,更加精进。 随便整理了一波废文件。
json.dumps
json.dumps的解释有一个博客写的很好,例子非常浅显易懂,这里借鉴思考。
先抛出答案,格式不同,一个是dict,一个是str。
dump不讲了,实际用得少,很废。
{'age': 24, 'name': 'jia'} <type 'dict'>
{"age": 24, "name": "jia"} <type 'str'>
因此这个地方的data本身是dict格式的,这样一搞就是str格式了。
str
json.loads就是反向操作。
<type 'str'>
<type 'dict'>
requests
下面要搞懂requests函数的意思,这里有一个很有用的入门介绍,主要用github的来测试。
对于get
<Response [200]> <class 'requests.models.Response'>
显然r什么都看不到。但是如果用浏览器可以看到是
{
"args": {},
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
"Connection": "close",
"Host": "httpbin.org",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
},
"origin": "69.172.67.65",
"url": "http://httpbin.org/get"
}
对于get常常需要加入params,这里不需要手动加,直接设置就好。
http://httpbin.org/get?key2=value2&key1=value1
这样就不需要手打问号了。
试试post
<Response [200]> <class 'requests.models.Response'>
要是想查看格式的话,用.text
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Content-Length": "0",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.14.2"
},
"json": null,
"origin": "69.172.67.65",
"url": "http://httpbin.org/post"
}
<type 'unicode'> {
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Content-Length": "0",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.14.2"
},
"json": null,
"origin": "69.172.67.65",
"url": "http://httpbin.org/post"
}
<type 'str'>
所以.content是帮助转化成str格式了。
其他
None
{u'files': {}, u'origin': u'69.172.67.65', u'form': {}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'0', u'Accept-Encoding': u'gzip, deflate', u'Host': u'httpbin.org', u'Accept': u'*/*', u'User-Agent': u'python-requests/2.14.2', u'Connection': u'close'}, u'json': None, u'data': u''} <type 'dict'>
这样就是字典格式了。
加入headers的.post情况。
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.post("http://httpbin.org/post", data=payload)
print r.text{
"args": {},
"data": "",
"files": {},
"form": {
"key1": "value1",
"key2": "value2"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Content-Length": "23",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.14.2"
},
"json": null,
"origin": "69.172.67.65",
"url": "http://httpbin.org/post"
}
还是有点不懂,到底是怎么取用的呢?
{'Content-Length': '463', 'X-Processed-Time': '0', 'X-Powered-By': 'Flask', 'Server': 'meinheld/0.6.1', 'Connection': 'keep-alive', 'Via': '1.1 vegur', 'Access-Control-Allow-Credentials': 'true', 'Date': 'Wed, 21 Mar 2018 12:27:14 GMT', 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json'}
实战
M站表
headers = {"Content-type": "application/json", "Accept": "application/json"}
timeout = 10
# url = "http://loansd.ppdapi.com/Lightning/QueryService/QueryListingAutoFlowUserProcess"for item in [24064633,24739192]:
data = {
"UserIds": [int(item)]
}
data1 = json.dumps(data, ensure_ascii=False)
response = requests.post(url, data1, headers=headers, timeout=timeout).text
response= response.encode('utf-8')str
但是我们熟悉的都是dict→pd.DataFrame。
这里保护用户隐私就不显示了。
这个地方主义response的第一个keys才是一个我们真正需要的dict。
那么多没有意义的变量,选择一下好了。我们需要时间,这个时候通过阅读cookbook知道,使用filter函数就好。
这里保护用户隐私就不显示了。
完美.
这种回传出来,是这种乱糟糟格式的是啥情况,不是str,也不是dict
芝麻分
headers = {"Content-type": "application/json", "Accept": "application/json"}
timeout = 10
# url = "http://3rdreadapi.ppdapi.com/queryData"
data = {
"options": "zhimaquery",
"version": "1.0.0",
"async": "1",
"atom": 0,
"role": 0
}
data = json.dumps(data)这个地方陈建说,data要限制为str格式,不能是dict。
否则,400 请求格式错误。 这是开发文档。
这里保护用户隐私就不显示了。