首页 > 解决方案 > 为什么变量可以添加到'.json'和'.text'

问题描述

import requests
req=requests.get(json_url)
with open('xxx','w') as f:

    f.write(req.text)
file_requests=req.json()

requests是一个模块,get是一个函数,所以req=requests.get(),但是req.txt和req.json()是什么,它也是一个函数吗?

标签: pythonpython-3.x

解决方案


这一行:

requests.get(json_url)

返回 python 对象(http://docs.python-requests.org/en/master/api/#requests.Response

requests.Response

  • 有方法json(以 json 形式返回 http-body)
  • 有属性(文本和内容)

可能是财产,(阅读https://www.python.org/dev/peps/pep-0549/


推荐阅读