一、requests
Requests 是使用 Apache2 Licensed 许可证的 基于Python开发的HTTP 库,其在Python内置模块的基础上进行了高度的封装,从而使得Pythoner进行网络请求时,变得美好了许多,使用Requests可以轻而易举的完成浏览器可有的任何操作。
1、安装模块
pip3 install requests
2、使用模块
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
(1)无参数实例 >>> import requests >>> ret = requests.get('https://www.baidu.com') >>> ret.url 'https://www.baidu.com/' >>> ret.text '<!DOCTYPE html> <!--STATUS OK--><html> <head><meta http-equiv=content-type c ontent=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge> <meta content=always name=referrer><link rel=stylesheet type=text/css href=https... (2)有参数实例 >>> ret = requests.get("http://httpbin.org/get", params=payload) >>> ret.url 'http://httpbin.org/get?key1=value1&key2=value2' >>> ret.text '{ "args": { "key1": "value1", "key2": "value2" }, "headers ": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Host ": "httpbin.org", "User-Agent": "python-requests/2.12.1" }, "origin ": "101.254.232.211", "url": "http://httpbin.org/get?key1=value1&key2=value2
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1、基本post实例 >>> payload = {'key1': 'value1', 'key2': 'value2'} >>> ret = requests.post("http://httpbin.org/post", data=payload) >>> ret.text '{ "args": {}, "data": "", "files": {}, "form": { "key1": "v alue1", "key2": "value2" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "23", "Conte nt-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "python-requests/2.12.1" }, "json": null, "origin": " 101.254.232.211", "url": "http://httpbin.org/post" } ' 2、发送请求头和数据实例 >>> import requests >>> import json >>> ... url = 'https://api.github.com/some/endpoint' >>> payload = {'some': 'data'} >>> headers = {'content-type': 'application/json'} >>> ... ret = requests.post(url, data=json.dumps(payload), headers=headers) >>> ret.text '{"message":"Not Found","documentation_url":"https://developer.github.com/v3"}' >>> ret.cookies <RequestsCookieJar[]>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
requests.get(url, params=None, **kwargs) requests.post(url, data=None, json=None, **kwargs) requests.put(url, data=None, **kwargs) requests.head(url, **kwargs) requests.delete(url, **kwargs) requests.patch(url, data=None, **kwargs) requests.options(url, **kwargs) # 以上方法均是在此方法的基础上构建 requests.request(method, url, **kwargs)
参考:http://www.cnblogs.com/wupeiqi/articles/5501365.html
http://cn.python-requests.org/zh_CN/latest/
二、logging
http://www.cnblogs.com/wupeiqi/articles/5501365.html
第十点logging日志模块