【问题标题】:Why does variable could be added to'.json' and '.text' [duplicate]为什么可以将变量添加到'.json'和'.text' [重复]
【发布时间】:2019-04-06 12:36:42
【问题描述】:
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()是什么,它也是一个函数吗?

【问题讨论】:

  • requests.get() 对指定的 URL 执行 get 请求,req.textrequests.get 返回的对象的属性,req.json() 是请求的内置 json 解码器方法。
  • 查看此文档requests
  • 欢迎。您可以从 link 看到 req.text 和从 link 看到 req.json()

标签: python python-3.x


【解决方案1】:

这一行:

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/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 2014-03-24
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多