GitHub API URL:https://developer.github.com/
- GET: 查看资源
- POST: 增加资源
- PUT: 修改资源
- DELETE: 删除资源
- HEAD: 查看响应头
- OPTIONS: 查看可用请求方法
requests.[method](url)
#!/usr/bin/env python # -*- coding:utf-8 -*- __Author__ = "HackFun" __Date__ = '2017/9/28 23:15' import requests import json URL = 'https://api.github.com' def build_uri(endpoint): return '/'.join([URL, endpoint]) def better_print(json_str): return json.dumps(json.loads(json_str), indent=4) def request_method(): response = requests.get(build_uri('user/emails'), auth=('[email protected]', 'qqq@123456')) print better_print(response.text) if __name__ == '__main__': request_method()
1.URL Parameters:URL参数
-
params:requests.get(url,params={'key1': 'value1'})
2.表单参数提交:
- Content-Type: application/x-www-form-urlencoded
- 内容: key1=value1&key2=value2
- request.post(url, data={'key1': 'value1', 'key2': 'value2'})
3.json参数提交
- Content-Type:application/json
- 内容: '{"key1": "value1", "key2": "value2"}'
- requests.post(url, json={'key1': 'value1', 'key2': 'value2'})
https://developer.github.com/v3/users/#parameters-1
def params_request(): response = requests.get(build_uri('users'), params={'since': 11}) print better_print(response.text) print response.request.headers print response.url
https://developer.github.com/v3/users/#update-the-authenticated-user
def json_request(): response = requests.patch(build_uri('user'), auth=('[email protected]', 'xxxx'), json={"name": 'hackfun', "email": "[email protected]"}) print better_print(response.text) print response.request.headers print response.request.body print response.status_code
代码添加email
def json_request(): response = requests.patch(build_uri('user/emails'), auth=('[email protected]', 'xxxx'), json=["[email protected]"]) print better_print(response.text) print response.request.headers print response.request.body print response.status_code
异常类型: BaseHTTPError ChunkedEncodingError ConnectionTimeout ConnectionError ContentDecodingError HTTPError InvalidSchema InvalidURL MissingSchema ProxyError ReadTimeout RequestException RetryError SSLError StreamConsumedError Timeout TooManyRedirects URLRequired
def timeout_request(): try: response = requests.get(build_uri('user/emails'), timeout=0,1) except exceptions.Timeout as e: print e.message else: print response.text
def timeout_request(): try: response = requests.get(build_uri('user/emails'), timeout=0.1) response.raise_for_status() except exceptions.Timeout as e: print e.message except exceptions.HTTPError as e: print e.message else: print response.text print response.status_code.code
class Request:的构造函数 __init__(self, method=None, url=None, headers=None, files=None, data=None, params=None, auth=None, cookies=None, hooks=None, json=None):
def hard_requests(): from requests import Session, Request s = Session() headers = {'User-Agent': 'fake1.3.4'} req = Request('GET', build_uri('user/emails'), auth=('[email protected]', 'xxxx'), headers=headers) prepped = req.prepare() print prepped.body print prepped.headers
def hard_requests(): from requests import Session, Request s = Session() headers = {'User-Agent': 'fake1.3.4'} req = Request('GET', build_uri('user/emails'), auth=('[email protected]', 'xxxx'), headers=headers) prepped = req.prepare() print prepped.body print prepped.headers resp = s.send(prepped, timeout=5) print resp.status_code print resp.request.headers print resp.text