Python Requests庫使用2:請求方法

GitHub API

URL: https://developer.github.com

HTTP verbs1

Where possible, API v3 strives to use appropriate HTTP verbs for each action.

Verb Description
HEAD Can be issued against any resource to get just the HTTP header info. 僅獲取HTTP響應頭信息。
GET Used for retrieving resources. 檢索資源
POST Used for creating resources2. 創建資源
PATCH Used for updating resources with partial JSON data. For instance, an Issue resource has title and body attributes. A PATCH request may accept one or more of the attributes to update the resource. PATCH is a relatively new and uncommon HTTP verb, so resource endpoints also accept POST requests. 用於使用部分JSON數據更新資源。
PUT Used for replacing resources or collections. For PUT requests with no body attribute, be sure to set the Content-Length header to zero. 替換資源或集合。
DELETE Used for deleting resources. 刪除資源

另外:

  • OPTIONS: 查看可用請求方法

使用

requests.[method](url)
request method——GET

https://developer.github.com/v3/users/ 的用法:

import json
import requests

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('users/onefine'))
    print(better_print(response.text))

if __name__ == '__main__':
    request_method()

輸出:

{
    "login": "onefine",
    "id": 15047276,
    "node_id": "MDQ6VXNlcjE1MDQ3Mjc2",
    "avatar_url": "https://avatars1.githubusercontent.com/u/15047276?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/onefine",
    "html_url": "https://github.com/onefine",
    "followers_url": "https://api.github.com/users/onefine/followers",
    "following_url": "https://api.github.com/users/onefine/following{/other_user}",
    "gists_url": "https://api.github.com/users/onefine/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/onefine/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/onefine/subscriptions",
    "organizations_url": "https://api.github.com/users/onefine/orgs",
    "repos_url": "https://api.github.com/users/onefine/repos",
    "events_url": "https://api.github.com/users/onefine/events{/privacy}",
    "received_events_url": "https://api.github.com/users/onefine/received_events",
    "type": "User",
    "site_admin": false,
    "name": "onefine",
    "company": null,
    "blog": "",
    "location": null,
    "email": null,
    "hireable": null,
    "bio": null,
    "public_repos": 5,
    "public_gists": 0,
    "followers": 0,
    "following": 1,
    "created_at": "2015-10-09T09:10:16Z",
    "updated_at": "2019-02-17T07:31:36Z"
}

更改:

def request_method():
    response = requests.get(build_uri('user/emails'), auth=('onefine', '******'))
    print(better_print(response.text))

結果:

[
    {
        "email": "[email protected]",
        "primary": true,
        "verified": true,
        "visibility": "public"
    }
]

帶參數的請求:

1. URL Parameters: URL參數
  • https://list.tmall.com/search_product.html?cat=50514037&...
  • params: requests.get(url, params={'key1', 'value1'})
2. 表單參數提交
  • Content-Type: application/x-www-form-urlencoded
  • 內容: key1=value1&key2=value2
  • requests.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'})
Get all users

Lists all users, in the order that they signed up on GitHub. This list includes personal user accounts and organization accounts.

Note: Pagination is powered exclusively by the since parameter. Use the Link header to get the URL for the next page of users.

GET /users

Parameters

Name Type Description
since string The integer ID of the last User that you’ve seen.

示例:

def params_request():
    response = requests.get(build_uri('users'), params={'since': 11})
    print(better_print(response.text))
    print(response.request.headers)
    print(response.url)


if __name__ == '__main__':
    params_request()

輸出:

[
    {
        "login": "vanpelt",
        "id": 17,
        "node_id": "MDQ6VXNlcjE3",
        "avatar_url": "https://avatars1.githubusercontent.com/u/17?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/vanpelt",
        "html_url": "https://github.com/vanpelt",
        "followers_url": "https://api.github.com/users/vanpelt/followers",
        "following_url": "https://api.github.com/users/vanpelt/following{/other_user}",
        "gists_url": "https://api.github.com/users/vanpelt/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/vanpelt/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/vanpelt/subscriptions",
        "organizations_url": "https://api.github.com/users/vanpelt/orgs",
        "repos_url": "https://api.github.com/users/vanpelt/repos",
        "events_url": "https://api.github.com/users/vanpelt/events{/privacy}",
        "received_events_url": "https://api.github.com/users/vanpelt/received_events",
        "type": "User",
        "site_admin": false
    },
    # ...省略
    {
        "login": "knzconnor",
        "id": 53,
        "node_id": "MDQ6VXNlcjUz",
        "avatar_url": "https://avatars3.githubusercontent.com/u/53?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/knzconnor",
        "html_url": "https://github.com/knzconnor",
        "followers_url": "https://api.github.com/users/knzconnor/followers",
        "following_url": "https://api.github.com/users/knzconnor/following{/other_user}",
        "gists_url": "https://api.github.com/users/knzconnor/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/knzconnor/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/knzconnor/subscriptions",
        "organizations_url": "https://api.github.com/users/knzconnor/orgs",
        "repos_url": "https://api.github.com/users/knzconnor/repos",
        "events_url": "https://api.github.com/users/knzconnor/events{/privacy}",
        "received_events_url": "https://api.github.com/users/knzconnor/received_events",
        "type": "User",
        "site_admin": false
    }
]
{'User-Agent': 'python-requests/2.21.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
https://api.github.com/users?since=11
Update the authenticated user
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.

Parameters
Name Type Description
name string The new name of the user.
email string The publicly visible email address of the user.
blog string The new blog URL of the user.
company string The new company of the user.
location string The new location of the user.
hireable boolean The new hiring availability of the user.
bio string The new short biography of the user.

def json_request():
    response = requests.patch(build_uri('user'), auth=('onefine', '******'),
                              json={'name': 'onefine',
                                    # 'email': '[email protected]',
                                    'blog': 'https://blog.csdn.net/jiduochou963',
                                    })
    print(better_print(response.text))
    print(response.request.headers)
    print(response.request.body)
    print(response.status_code)


if __name__ == '__main__':
    json_request()

輸出:

{
    "login": "onefine",
    "id": 15047276,
    "node_id": "MDQ6VXNlcjE1MDQ3Mjc2",
    "avatar_url": "https://avatars1.githubusercontent.com/u/15047276?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/onefine",
    "html_url": "https://github.com/onefine",
    "followers_url": "https://api.github.com/users/onefine/followers",
    "following_url": "https://api.github.com/users/onefine/following{/other_user}",
    "gists_url": "https://api.github.com/users/onefine/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/onefine/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/onefine/subscriptions",
    "organizations_url": "https://api.github.com/users/onefine/orgs",
    "repos_url": "https://api.github.com/users/onefine/repos",
    "events_url": "https://api.github.com/users/onefine/events{/privacy}",
    "received_events_url": "https://api.github.com/users/onefine/received_events",
    "type": "User",
    "site_admin": false,
    "name": "onefine",
    "company": null,
    "blog": "https://blog.csdn.net/jiduochou963",
    "location": null,
    "email": "[email protected]",
    "hireable": null,
    "bio": null,
    "public_repos": 5,
    "public_gists": 0,
    "followers": 0,
    "following": 1,
    "created_at": "2015-10-09T09:10:16Z",
    "updated_at": "2019-02-23T06:44:38Z",
    "private_gists": 0,
    "total_private_repos": 0,
    "owned_private_repos": 0,
    "disk_usage": 0,
    "collaborators": 0,
    "two_factor_authentication": false,
    "plan": {
        "name": "free",
        "space": 976562499,
        "collaborators": 0,
        "private_repos": 10000
    }
}
{'User-Agent': 'python-requests/2.21.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Length': '65', 'Content-Type': 'application/json', 'Authorization': 'Basic b25lZmluZTpTaWxlbnQ5NjMxMjM='}
b'{"name": "onefine", "blog": "https://blog.csdn.net/jiduochou963"}'
200

添加email:

def json_request():
    response = requests.post(build_uri('user/emails'), auth=('onefine', '******'),
                             json=['[email protected]'])

    print(better_print(response.text))
    print(response.request.headers)
    print(response.request.body)
    print(response.status_code)


if __name__ == '__main__':
    json_request()

輸出:

[
    {
        "email": "[email protected]",
        "primary": true,
        "verified": true,
        "visibility": "public"
    },
    {
        "email": "[email protected]",
        "primary": false,
        "verified": false,
        "visibility": null
    }
]
{'User-Agent': 'python-requests/2.21.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Length': '27', 'Content-Type': 'application/json', 'Authorization': 'Basic b25lZmluZTpTaWxlbnQ5NjMxMjM='}
b'["[email protected]"]'
201

參考:
python的擴展包requests的高級用法: https://www.cnblogs.com/guhao123/p/4054177.html


  1. https://developer.github.com/v3/#http-verbs ↩︎

  2. 有時候用於修改資源,當這並不符合RESTful規範,更好的方法是PATCH↩︎

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章