本文共 869 字,大约阅读时间需要 2 分钟。
在Python中,urllib是一个强大的工具,用于处理HTTP请求。以下是一个简单的示例,展示了如何通过urllib库发送HTTP请求并获取响应。
import urllib.requestimport urllib.parseurl = 'http://www.nnzhp.cn'request = urllib.request.urlopen(url)content = request.read().decode()
import jsonurl = 'http://api.nnzhp.cn/api/user/stu_info?stu_name=xiaohei'request = urllib.request.urlopen(url)content = request.read().decode()result = json.loads(content)if result.get('error_code') == 0: print('测试通过')else: print('测试失败', result) url = 'http://api.nnzhp.cn/api/user/login'data = { 'username': 'admin', 'passwd': 'aA123456'}data_encoded = urllib.parse.urlencode(data)request = urllib.request.urlopen(url, data_encoded.encode())response = request.read().decode()print(response) 通过上述代码示例,可以看到如何在Python中使用urllib库来发送GET和POST请求,并处理服务器返回的响应。如果需要更多功能,可以参考Python官方文档或第三方库如requests。
转载地址:http://wklfk.baihongyu.com/