使⽤用Requests庫構建⼀一個HTTP請求

一隻要飛的豬發表於2019-04-21

請求方法

  • GET 檢視資源
  • POST 增加資源
  • PUT 修改資源
  • DELETE 刪除資源
  • HEAD 檢視響應頭
  • OPTIONS 檢視可用請求方法 requests.[method](url)

GitHub API 示例

https://developer.github.com/v3/migrations/users/

  • json.load()將已編碼的 JSON 字串解碼為 Python 物件
def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None,
        parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
    """Deserialize ``s`` (a ``str`` instance containing a JSON
    document) to a Python object.
複製程式碼
  • json.dumps()將 Python 物件編碼成 JSON 字串
def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
        allow_nan=True, cls=None, indent=None, separators=None,
        default=None, sort_keys=False, **kw):
    """Serialize ``obj`` to a JSON formatted ``str``.
複製程式碼
  • encode將Python物件編碼成JSON字串
  • decode將已編碼的JSON字串解碼為Python物件
    使⽤用Requests庫構建⼀一個HTTP請求
URL = 'https://api.github.com'

def build_uri(endpoint):
    return '/'.join([URL, endpoint])

def better_print(json_str):
    """Deserialize ``s`` (a ``str`` instance containing a JSON
        document) to a Python object."""
    return json.dumps(json.loads(json_str), indent=4)

def request_method():
    response = requests.get(build_uri('user/emails'), auth=('user', 'psw'))
    print(response.status_code)
    print(better_print(response.text))
>>[
  {
    "email": "octocat@github.com",
    "verified": true,
    "primary": true,
    "visibility": "public"
  }
]
複製程式碼

Github修改使用者資訊

  • PATCH /user
  • Note: If your email is set to private and you send an email parameter as part of this request to update your profile, your privacy settings are still enforced: the email address will not be displayed on your public profile or via the API.
  • response = requests.patch(url, auth=('user', 'psw'), json={'name':'123'})

請求異常處理

request.get(url,timeout=timeout) #timeout=(x1,x2) 每步單獨限制request-response時長 #timeout=x 總體限制request-response時長

from requests import exceptions
def timeout_request():
    try:
        response = requests.get(build_uri('user/emails'),timeout=10)
    except exceptions.Timeout as e:
        print(str(e))
    else:
        print(response.text)
複製程式碼

自定義Requests

使⽤用Requests庫構建⼀一個HTTP請求

def hard_request():
    from requests import  Request, Session
    s = Session()
    headers = {'User-Agent': 'fake1.3.4'}
    req = Request('GET',build_uri('user/emails'), auth=('user','psw'),
                  headers = headers)
    prepped = req.prepare()
    print(prepped.body)
    print(prepped.headers)
    resp = s.send(prepped, timeout=5)
    print(resp.status_code)
    print(resp.headers)
    print(resp.text)
複製程式碼

關於User-Agent

User-Agent會告訴網站伺服器,訪問者是通過什麼工具來請求的,如果是爬蟲請求,一般會拒絕,如果是使用者瀏覽器,就會應答。

相關文章