先引入包
import urllib.request import ssl import json
下文中context是https方式用到
context = ssl._create_unverified_context()
1.get
request = urllib.request.Request(url) response = urllib.request.urlopen(url=request,context=context) dic = response.read().decode(‘utf-8‘)
2.post
headers = {}headers[‘Content-Type‘] = ‘application/json; charset=utf-8‘
#j_data是json结构化数据
values = {}
values["token"] =token
values["CustomApp"]="Web"
j_data = json.dumps(values)#结构化数据之后才发送请求 def jsonPost(url,j_data): request = urllib.request.Request(url,bytes(j_data,‘utf8‘), headers,method=‘POST‘)#bytes:把请求内容转成bytes才能发送,否则报错 response = urllib.request.urlopen(url=request,context=context) dic = response.read().decode(‘utf-8‘) response.close() return dic
3.请求结果json结构化
js = json.loads(dic)
原文地址:https://www.cnblogs.com/cvol/p/11017598.html
时间: 2024-10-13 04:41:00