python模块(requests,logging)

一、requests

Requests 是使用 Apache2 Licensed 许可证的 基于Python开发的HTTP 库,其在Python内置模块的基础上进行了高度的封装,从而使得Pythoner进行网络请求时,变得美好了许多,使用Requests可以轻而易举的完成浏览器可有的任何操作。

1、安装模块

pip3 install requests

2、使用模块

(1)无参数实例

>>> import requests

>>> ret = requests.get('https://www.baidu.com')
>>> ret.url
'https://www.baidu.com/'
>>> ret.text
'<!DOCTYPE html>\r\n<!--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
'{\n "args": {\n "key1": "value1", \n "key2": "value2"\n }, \n "headers
": {\n "Accept": "*/*", \n "Accept-Encoding": "gzip, deflate", \n "Host
": "httpbin.org", \n "User-Agent": "python-requests/2.12.1"\n }, \n "origin
": "101.254.232.211", \n "url": "http://httpbin.org/get?key1=value1&key2=value2
Get请求
1、基本post实例
>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> ret = requests.post("http://httpbin.org/post", data=payload)
>>> ret.text
'{\n  "args": {}, \n  "data": "", \n  "files": {}, \n  "form": {\n    "key1": "v
alue1", \n    "key2": "value2"\n  }, \n  "headers": {\n    "Accept": "*/*", \n
  "Accept-Encoding": "gzip, deflate", \n    "Content-Length": "23", \n    "Conte
nt-Type": "application/x-www-form-urlencoded", \n    "Host": "httpbin.org", \n
  "User-Agent": "python-requests/2.12.1"\n  }, \n  "json": null, \n  "origin": "
101.254.232.211", \n  "url": "http://httpbin.org/post"\n}\n'

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[]>
post请求
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日志模块

 

posted @ 2017-03-01 15:04  willpower-chen  阅读(982)  评论(0编辑  收藏  举报