一、请求方式:get、post、put、delete、head、options
import requests #http协议 url='http://httpbin.org/get' payload={'key1':'value2','key2':'value2'} # get请求传参数时,使用params关键字 # post请求传参数时,使用data关键字 # timeout 等待响应的时间 # allow_redirects=True 允许重定向 # json=body body是json格式的 # data=json.dumps(body) 将dict转化成json字符串 r=requests.get(url, params=payload,timeout=5) print('返回响应体:', r.text) print('以JSON格式解析响应内容:', r.json()) print('返回状态码:', r.status_code) print('打印cookies:', r.cookies) print('获取原来创建的Request对象:', r.request) print('响应头:', r.headers) print('请求头:', r.request.headers) print('响应头Connection:',r.headers.get('Connection')) print('响应头Content-Type:',r.headers['Content-Type']) #如果发送了一个错误请求,如404、500等,可以通过raise_for_status()来抛出异常 print('错误码:', r.raise_for_status()) #查看requests使用了什么编码,同时可以用r.encoding属性来改变它 print('编码前:',r.encoding) r.encoding='gbk' print('编码后:',r.encoding) print('重定向历史:',r.history) print('断言:', r.status_code == requests.codes.ok)
二、cookies
应用:通过fiddler抓包获取登录前后cookies的差异,然后将差异set到刚登录的cookies中,再次请求就能登录了。
import requests url='http://httpbin.org/cookies' cookies=dict(cookies_are='working') r=requests.get(url, cookies=cookies) jar=requests.cookies.RequestsCookieJar() print('----响应结果---',r.text) print('----设置前---',jar) jar.set('tasty_cookie', 'yum', domain='httpbin.org', path='/cookies') jar.set('gross_cookie', 'blech', domain='httpbin.org', path='/cookies') #设置cookies并请求 r = requests.get(url, cookies=jar) print('----设置后---',jar) print('----响应结果---',r.text)
三、上传文件
import requests url = 'http://httpbin.org/post' dir=r'C:Users123Desktop est.txt' #上传多部分编码文件 files1 = {'file': open(dir, 'rb')} r = requests.post(url, files=files1) print('---1---',r.text) #显式地设置文件名,文件类型和请求头 files2 = {'file': ('test.txt', open(dir, 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})} r = requests.post(url, files=files2) print('---2---',r.text) #发送作为文件来接收的字符串 files3 = {'file': (dir, 'some,data,to,send another,row,to,send ')} r = requests.post(url, files=files3) print('---3---',r.text)
四、xml转json
from xml.parsers.expat import ParserCreate class Xml2Json: LIST_TAGS = ['COMMANDS'] def __init__(self, data=None): self._parser = ParserCreate() self._parser.StartElementHandler = self.start self._parser.EndElementHandler = self.end self._parser.CharacterDataHandler = self.data self.result = None if data: self.feed(data) self.close() def feed(self, data): self._stack = [] self._data = '' self._parser.Parse(data, 0) def close(self): self._parser.Parse("", 1) del self._parser def start(self, tag, attrs): assert attrs == {} assert self._data.strip() == '' self._stack.append([tag]) self._data = '' def end(self, tag): last_tag = self._stack.pop() assert last_tag[0] == tag if len(last_tag) == 1: # leaf data = self._data else: if tag not in Xml2Json.LIST_TAGS: # build a dict, repeating pairs get pushed into lists data = {} for k, v in last_tag[1:]: if k not in data: data[k] = v else: el = data[k] if type(el) is not list: data[k] = [el, v] else: el.append(v) else: # force into a list data = [{k: v} for k, v in last_tag[1:]] if self._stack: self._stack[-1].append((tag, data)) else: self.result = {tag: data} self._data = '' def data(self, data): self._data = data
例子:
url = 'http://api.map.baidu.com/telematics/v3/weather?location=武汉&ak=8IoIaU655sQrs95uMWRWPDIa' r = requests.post(url) # 获取返回结果 m = r.text print('获取返回结果:',m) result=Xml2Json(m).result print('将返回结果转换json:',result) print('断言:',result['CityWeatherResponse']['error'] == '0')