python - 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)
关于我的问题,我找到了这段代码。这是正确的方法吗?请帮我解决这个问题。
解决方案
你可以使用requests
,没有理智的人使用 urllib。
安装它:
$ pip install requests
然后像这样使用它:
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
推荐阅读
- httpclient - HttpClient5.0,如何在异步模式下使用gzip?
- java - 为什么我无法处理 Kafka 消息?
- vba - 如何在通过电子邮件发送附件之前获取附件的文件名?
- php - Laravel - 未定义变量:供应商(查看:C:\wamp64\www\oficina2.0\oficina2.0\oficina\resources\views\index.blade.php)
- c# - 如何使安装应用程序时数据库不可见?
- php - 如何为视图中的非唯一记录设置相同的颜色
- flutter - 为什么我的资产图像第一次加载但第二次在我用颤振构建我的应用程序时加载
- python - PyCharm 无法导入模块
- python - 在 kivymd 中使用 arabic_reshaper 和 bidi.algorithm
- excel - 如何为列中的每个“新”值创建一个新的工作簿?