这里主要记录一下requests模块的以下几点:python
更详细的使用参见官方文档:http://docs.python-requests.org/zh_CN/latest/post
requests模块数据第三方库,这里使用pip进行安装:
pip install requests编码
requests.get(url=url, headers=headers, params=params)url
# coding:utf-8 import requests # 请求url url = "http://httpbin.org/get" # 请求头 headers = { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "User-Agent": "python-requests/2.9.1", } # 查询字符串 params = {'name': 'Jack', 'age': '24'} r = requests.get(url=url, headers=headers, params=params) print r.status_code # 获取响应状态码 print r.content # 获取响应消息 if __name__ == "__main__": pass
requests.post(url=url, headers=headers, data=params)spa
# coding:utf-8 import requests # 请求url url = "http://httpbin.org/post" # 请求头 headers = { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "User-Agent": "python-requests/2.9.1", } # 查询字符串 params = {'name': 'Jack', 'age': '24'} r = requests.post(url=url, headers=headers, data=params) print r.status_code # 获取响应状态码 print r.content # 获取响应消息 if __name__ == "__main__": pass
requests.post(url=url, headers=headers, data=params, files=files)
参数说明:code
# coding:utf-8 import requests # 请求url url = "http://httpbin.org/post" # 请求头 headers = { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "User-Agent": "python/2.9.1", } # 查询字符串 params = {'name': 'Jack', 'age': '24'} # 文件 files = {'upload_img': ('report.xlsx', open('report.xlsx', 'rb'), 'image/png')} r = requests.post(url=url, data=params, headers=headers, files=files) print r.status_code # 获取响应状态码 print r.content # 获取响应消息 if __name__ == "__main__": pass