python - Python 中的 Web 服务?

标签 python python-3.x python-requests urllib

如何将 JSON Web 服务/Web Api 与我们的 python 程序集成以及 程序应在运行时从配置文件获取配置设置

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

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

最佳答案

您可以使用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
    

关于python - Python 中的 Web 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57336798/

相关文章:

python - 无法嗅探以太网类型 0x0102 自定义数据包

c++ - 将列表从 python 传递到 C++ vector 时如何加速 Boost::Python::Extract

python - 无法在 Linux 上安装 Python3

python - Numba python3 出错 [GPU ufunc 要求数组参数具有准确的类型。]

python - 在 Ubuntu 中安装 GDAL Python 绑定(bind)以用作独立模块

python - 属性错误 : '_socketobject' object has no attribute 'set_tlsext_host_name'

python - 使用 python 与本地 HTML 文件交互

python - python的matplotlib中如何获取当前图号?

python - 安装Anaconda后在Linux Mint中配置python3指向/usr/bin/python3.8

python - 在 Python 中根据请求发送 PIL 图像