GitHub API

HTTP verbs

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 resources

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

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": "188302531@qq.com",

"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': 'hello-world@onefine.com',

'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": "188302531@qq.com",

"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=['hello-world@onefine.com'])

print(better_print(response.text))

print(response.request.headers)

print(response.request.body)

print(response.status_code)

if __name__ == '__main__':

json_request()

输出:

[

{

"email": "188302531@qq.com",

"primary": true,

"verified": true,

"visibility": "public"

},

{

"email": "hello-world@onefine.com",

"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'["hello-world@onefine.com"]'

201

Requests库-请求异常处理

from requests import exceptions

exceptions.BaseHTTPError

exceptions.ChunkedEncodingError

exceptions.ConnectTimeout

exceptions.ConnectionError

exceptions.ContentDecodingError

exceptions.HTTPError

exceptions.InvalidHeader

exceptions.InvalidURL

exceptions.MissingSchema

exceptions.ProxyError

exceptions.ReadTimeout

exceptions.RequestException

exceptions.RetryError

exceptions.SSLError

exceptions.StreamConsumedError

exceptions.Timeout

exceptions.TooManyRedirects

exceptions.URLRequired

超时为例:

requests.get(url, timeout=(3, 7))

requests.get(url, timeout=10)

from requests import exceptions

def timeout_request():

try:

response = requests.get(build_uri('user/emails'), timeout=0.1)

except exceptions.Timeout as e:

print("连接超时:", e)

else:

print(response.text)

if __name__ == '__main__':

timeout_request()

输出:

连接超时: HTTPSConnectionPool(host='api.github.com', port=443): Read timed out. (read timeout=0.1)

修改为正常时间:

from requests import exceptions

def timeout_request():

try:

response = requests.get(build_uri('user/emails'), timeout=10)

except exceptions.Timeout as e:

print("连接超时:", e)

else:

print(response.text)

print(response.status_code)

if __name__ == '__main__':

timeout_request()

输出:

{"message":"Requires authentication","documentation_url":"https://developer.github.com/v3/users/emails/#list-email-addresses-for-a-user"}

401

处理异常:

from requests import exceptions

def timeout_request():

try:

response = requests.get(build_uri('user/emails'), timeout=10)

# 显式抛出异常:

response.raise_for_status()

except exceptions.Timeout as e:

print("连接超时:", e)

except exceptions.HTTPError as e:

print("HTTPError: ", e)

else:

print(response.text)

print(response.status_code)

if __name__ == '__main__':

timeout_request()

输出:

HTTPError: 401 Client Error: Unauthorized for url: https://api.github.com/user/emails

庖丁解牛——自定义Request

读源码,认清复杂事务背后简单的原理。

def hard_requests():

from requests import Request, Session

s = Session()

headers = {'User-agent': 'fake1.3.4'}

req = Request('GET', build_uri('user/emails'), auth=('onefine', 'Silent963123'), headers=headers)

prepped = req.prepare()

print(prepped.body)

print(prepped.headers)

if __name__ == '__main__':

hard_requests()

输出:

None

{'User-agent': 'fake1.3.4', 'Authorization': 'Basic b25lZmluZTpTaWxlbnQ5NjMxMjM='}

接着:

def hard_requests():

from requests import Request, Session

s = Session()

headers = {'User-agent': 'fake1.3.4'}

req = Request('GET', build_uri('user/emails'), auth=('onefine', 'Silent963123'), 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)

if __name__ == '__main__':

hard_requests()

输出:

None

{'User-agent': 'fake1.3.4', 'Authorization': 'Basic b25lZmluZTpTaWxlbnQ5NjMxMjM='}

200

{'User-agent': 'fake1.3.4', 'Authorization': 'Basic b25lZmluZTpTaWxlbnQ5NjMxMjM='}

[{"email":"188302531@qq.com","primary":true,"verified":true,"visibility":"public"},{"email":"hello-world@onefine.com","primary":false,"verified":false,"visibility":null}]

python requests请求方式_Python Requests库使用2:请求方法相关推荐

  1. python包导入方式_python导包的几种方法(自定义包的生成以及导入详解)

    python 导入数据包的几种方法 1.直接导入整个数据包:improt 数据包 2.导入数据包中的某一个函数: from 数据包 improt 函数(当函数这一项为 * 时为导入整个数据包) 3. ...

  2. python requests是什么_python requests库学习

    Requests python的request库官方介绍就是让HTTP服务人类,所以从这点我们就可以知道request库是为了让我们更加方便的进行http相关的各种操作 我们学习request有什么用 ...

  3. python构造响应头_Python爬虫库requests获取响应内容、响应状态码、响应头

    首先在程序中引入Requests模块 import requests 一.获取不同类型的响应内容 在发送请求后,服务器会返回一个响应内容,而且requests通常会自动解码响应内容 1.文本响应内容 ...

  4. python绘制蚊香形_Python requests发送post请求的一些疑点

    前言 在Python爬虫中,使用requests发送请求,访问指定网站,是常见的做法.一般是发送GET请求或者POST请求,对于GET请求没有什么好说的,而发送POST请求,有很多朋友不是很清楚,主要 ...

  5. python网络请求模块_python学习笔记-day8-3-【python 网络请求及requests模块】

    importrequestsimportrandom#1.发get请求#url = 'http://xxxxxx/api/user/stu_info'#data = {'stu_name':'xxxx ...

  6. python requests session刷新_Python Requests Session set-cookie不生效的坑

    我们知道 Python Requests库 中的 Session 模块有连接池和会话管理的功能,比如请求一个登录接口后,会自动处理 response 中的 set-cookie,下次再请求时会自动把 ...

  7. python requests 代理超时_python requests 超时与重试

    一 源起: requests模块作为python爬虫方向的基础模块实际上在日常实际工作中也会涉及到,比如用requests向对方接口url发送POST请求进行推送数据,使用GET请求拉取数据. 但是这 ...

  8. 工信部python证书多少钱_python requests SSL证书问题

    错误信息如下: requests.exceptions.SSLError: ("bad handshake: Error([('SSL routines', 'tls_process_ser ...

  9. python 图形库有哪些_python常用库有哪些

    今天将介绍20个常用工具的Python库,我相信你看完之后也会觉得离不开它们. 它们是:(推荐学习:Python视频教程) Requests.Kenneth Reitz写的最富盛名的http库.每个P ...

最新文章

  1. OpenCV中resize函数五种插值算法的实现过程
  2. 【Python】Pandas中的宝藏函数-applymap
  3. 编程语言的发展趋势及未来方向(4):动态语言
  4. Java笔记12-函数式接口
  5. 句句属实,90%的人都被需求整“哭”过!
  6. 合并排序 非递归 java_合并排序-非递归
  7. E - 白银 CSU - 1726: 你经历过绝望吗?两次! 搜索
  8. 2012浙江大学光华法学院毕业典礼教师发言
  9. 延迟秋招总结,什么工作可以月薪过万?
  10. Scratch软件编程等级考试一级——20210911
  11. 相对于父容器(取值:true / false)帧布局(FrameLayout)
  12. python爬app西瓜视频_Python爬虫下载西瓜视频
  13. 考研政治---马克思主义基本原理概论---唯物史观
  14. 空间存储公链(SSCC)主链已进入公测阶段预计2020年初上线
  15. linux服务器安装openwrt,探索openwrt安装宝塔,搭建web网站论坛社区网校
  16. 在项目中使用Liquibase
  17. 2017 Multi-University Training Contest 1 solutions BY 北京航空航天大学
  18. html 预览 base64 PDF
  19. 微信小程序wxs的理解
  20. gbq可以算出土建量吗_广联达土建算量软件问题合集100条(上)

热门文章

  1. Apple - Bonjour (NSNetService)
  2. 显著性检测的评价指标代码
  3. AcWing 3443. 学分绩点
  4. A Word Can Make You Miss Your Deadline
  5. VS Code 插件:Better Align以及快捷键配置 代码对齐/代码网格化 /界面对齐/代码自动缩进
  6. G20伦敦峰会公告全文
  7. 大家知道微信被举报以后,多久才恢复正常吗?
  8. 【亚马逊运营】如何对产品进行市场调研,寻找盈利契机?
  9. Android仿QQ实现聊天功能
  10. git github