首页 > 解决方案 > Python 中的 Web 服务?

问题描述

如何将 JSON Web Services/Web Api 与我们的 python 程序集成,程序应该在运行时从配置文件中获取配置设置

 import urllib.request
 a_url = 'http://www.w3.org/2005/Atom'
 data = urllib.request.urlopen(a_url).read()
 print(data)

关于我的问题,我找到了这段代码。这是正确的方法吗?请帮我解决这个问题。

标签: pythonpython-3.xpython-requestsurllib

解决方案


你可以使用requests,没有理智的人使用 urllib。

  1. 安装它:

    $ pip install requests
    
  2. 然后像这样使用它:

    import requests
    a_url = 'http://www.w3.org/2005/Atom'
    response = requests.get(a_url)
    response.status_code  # 200
    response.text  # html of the webpage
    # response.json()  # only if the url responds with json data, otherwise it will throw error
    

推荐阅读