知识点一、网络请求1.1基础的get请求和post请求处理方式--get请求方式
from urllib import request
from urllib import parse
import json
#get请求
url = 'http://*****'
data = {"stu_name":"xiaohei"}
tmpData = parse.urlencode(data)#将数据变成kv k= v
#接口+参数
tmpUrl= url + '?'+ tmpData
print(tmpUrl)
res = request.urlopen(tmpUrl)
resForRead = res.read()#bytes类型
resString = resForRead.decode()#转换成string类型
print(type(resString))
#想得到返回数据的任意的变量
resForDict = json.loads(resString)#将字符串转成字典
print(resForDict)
1.1基础的get请求和post请求处理方式--post请求方式
from urllib import request
from urllib import parse
import json
url = 'http://****'
data = {"username":'niuhanyang','passwd':'aA123456'}
tmpData = parse.urlencode(data)#将数据变成kv k= v
res = request.urlopen(url,tmpData.encode())# post 请求 参数1 为接口地址 参数2位参数要求bytes
resForRead = res.read()#bytes类型
resString = resForRead.decode()#转换成string类型
print(type(resString))
#想得到返回数据的任意的变量
resForDict = json.loads(resString)#将字符串转成字典
print(resForDict)# post 和 get的区别在与urlopen时,get发的是接口和参数的平成字符串
1.2 使用requests模块优化网络请求的代码
import requests
#get请求
url = 'http://****'
data = {"stu_name":"xiaohei"}
#res = requests.get(url,data).text#text 方法返回的是字符串格式的返回值
res = requests.get(url,data).json()#json 方法返回的是字典格式的返回值
print(res)
print(type(res))
#post 请求
import requests
url = 'http://****'
d