import urllib.request import requests from requests.auth import HTTPBasicAuth from requests.auth import HTTPDigestAuth import json base_url = 'http://httpbin.org' # 发送get请求 r = requests.get(base_url+'/get') print(r.status_code) #发送post请求 r = requests.post(base_url+'/post') print(r.status_code) #发送put请求 r = requests.put(base_url+'/put') print(r.status_code) #发送delete请求 r = requests.delete(base_url+'/delete') print(r.status_code) # 发送get请求 传递url参数 param_data={'user':'zxw123','password':'666'} r = requests.get(base_url+'/get',params=param_data) print(r.url) print(r.status_code) resonponse = urllib.request.urlopen("http://baidu.com") html= resonponse print(html) # post传递body参数 from_data={'user':'zxw123','password':'8888'} r = requests.post(base_url+'/post',data = from_data) print(r.status_code) print(r.text) # 请求头定制 将dict传递给header from_data={'user':'zxw123','password':'8888'} header={'user-agent': 'Mozilla/5.0'} r = requests.post(base_url+'/post',data = from_data,headers = header) print(r.status_code) print(r.text) # 响应内容 from_data={'user':'zxw123','password':'8888'} header={'user-agent': 'Mozilla/5.0'} r = requests.post(base_url+'/post',data = from_data,headers = header) # 响应状态码 print(r.status_code) # 响应内容 print(r.text) # 响应内容以json格式 print(r.json()) # 响应头信息 print(r.headers) # 设置cookie cookie={'uesr':'beijing'} r=requests.get(base_url+'/cookies',cookies = cookie) print(r.text) # 获取cookie # 请求百度首页,获取cookie r = requests.get('http://baidu.com') print(type(r.cookies)) print(r.cookies) for key,value in r.cookies.items(): print(key+':'+value) # 超时 # 设定timeout超时之后停止响应,防止请求没有响应而一直处于等待状态 cookie={'uesr':'beijing'} r=requests.get(base_url+'/cookies',cookies = cookie,timeout = 10) print(r.text) # 文件上传 参数files file={'file':open('zxw_logo.png','rb')} r = requests.post(base_url+'/post',files=file) print(r.text) # session(会话) # 接口之间经常有依赖关系,在没有session保存机制的情况下,第二个接口无法获取第一个接口的cookie值 # 设置cookie r = requests.get(base_url+'/cookies/set/user/51zxw') print(r.text) # 获取cookie r = requests.get(base_url+'/cookies') print(r.text) # 加入session之后,可以模拟同一对话而不用担心cookies问题,通常用于模拟成功登陆之后进行下一步操作 s = requests.Session() # 设置cookie r=s.get(base_url+'/cookies/set/user/51zxw') print(r.text) # 获取cookie r =s.get(base_url+'/cookies') print(r.text) # ssl验证默认开启,验证失败会抛出SSLError # r = requests.get('https://www.12306.cn') # 关闭ssl验证 r=requests.get('https://www.12306.cn',verify=False) print(r.text) # 身份认证 BasicAuth r = requests.get(base_url+'/basic-auth/51zxw/8888',auth=HTTPBasicAuth('51zxw','8888')) print(r.text) # 身份认证 DigestAuth r = requests.get(base_url+'/digest-auth/auth/heiheiehi/23333',auth=HTTPDigestAuth('heiheihei','23333')) print(r.text) # # 代理设置 proxies = {'http':'http://219.142.132.146:4065'} r = requests.get(base_url+'/get',proxies=proxies) print(r.text) # 流式请求, 接口返回多个结果 r = requests.get(base_url+'/stream/10',stream = True) # 如果响应内容没有设置编码,则默认为utf-8 if r.encoding is None: r.encoding='utf-8' # 对响应结果进行迭代处理 for line in r.iter_lines(decode_unicode=True): if line: data=json.loads(line) print(data['id'])