requests安装
先看下怎么安装requests, 执行以下命令:

pip install requests

安装好后如何导入requests模块呢? 如下所示:

import requests

基本示例
下面我们看一个基本的示例, 体验下requests的强大, 直接上代码演示利用requests访问github的api, 具体api说明请参见:

https://developer.github.com/v3

代码示例

# 导入模块
import requests
if __name__ == "__main__":
print("fighter007 - requests示例")
# 发送HTTP GET请求, 获取github API列表
r = requests.get("https://api.github.com")
# 请求返回码
status_code = r.status_code
# 完整的返回头
headers = r.headers
# 请求返回头 content-type的值
content_type = r.headers["content-type"]
# 返回内容编码类型
code = r.encoding
# 返回内容文本
text = r.text
# 若返回结果为json格式, 我们可以获取其json格式内容
json_data = r.json()
# 打印上述所有获取到的值
print("状态码: ", status_code)
print("返回头: ", headers)
print("content-type: ", content_type)
print("编码: ", code)
print("文本内容: ", text)
print("json串内容: ", json_data)

将上述代码保存至requests_demo.py中, 执行下属命令运行:

Fighter007 - requests示例
状态码: 200
返回头: {'Strict-Transport-Security': 'max-age=31536000; includeSubdomains; preload', 'X-RateLimit-Limit': '', 'Content-Encoding': 'gzip', 'Content-Security-Policy': "default-src 'none'", 'X-RateLimit-Reset': '', 'Access-Control-Allow-Origin': '*', 'X-GitHub-Media-Type': 'github.v3; format=json', 'ETag': 'W/"7dc470913f1fe9bb6c7355b50a0737bc"', 'Date': 'Mon, 19 Feb 2018 01:32:20 GMT', 'Status': '200 OK', 'Vary': 'Accept, Accept-Encoding', 'X-Frame-Options': 'deny', 'Server': 'GitHub.com', 'X-Runtime-rack': '0.012498', 'Access-Control-Expose-Headers': 'ETag, Link, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval', 'Transfer-Encoding': 'chunked', 'X-XSS-Protection': '1; mode=block', 'X-Content-Type-Options': 'nosniff', 'X-GitHub-Request-Id': 'CB9C:6BE0:18167B6:1F2AC7F:5A8A2923', 'Content-Type': 'application/json; charset=utf-8', 'X-RateLimit-Remaining': '', 'Cache-Control': 'public, max-age=60, s-maxage=60'}
content-type: application/json; charset=utf-8
编码: utf-8
文本内容: {"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}
json串内容: {'public_gists_url': 'https://api.github.com/gists/public', 'user_repositories_url': 'https://api.github.com/users/{user}/repos{?type,page,per_page,sort}', 'authorizations_url': 'https://api.github.com/authorizations', 'organization_repositories_url': 'https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}', 'commit_search_url': 'https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}', 'gists_url': 'https://api.github.com/gists{/gist_id}', 'organization_url': 'https://api.github.com/orgs/{org}', 'following_url': 'https://api.github.com/user/following{/target}', 'events_url': 'https://api.github.com/events', 'rate_limit_url': 'https://api.github.com/rate_limit', 'starred_gists_url': 'https://api.github.com/gists/starred', 'team_url': 'https://api.github.com/teams', 'keys_url': 'https://api.github.com/user/keys', 'repository_search_url': 'https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}', 'code_search_url': 'https://api.github.com/search/code?q={query}{&page,per_page,sort,order}', 'emails_url': 'https://api.github.com/user/emails', 'notifications_url': 'https://api.github.com/notifications', 'current_user_authorizations_html_url': 'https://github.com/settings/connections/applications{/client_id}', 'current_user_repositories_url': 'https://api.github.com/user/repos{?type,page,per_page,sort}', 'starred_url': 'https://api.github.com/user/starred{/owner}{/repo}', 'feeds_url': 'https://api.github.com/feeds', 'issue_search_url': 'https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}', 'emojis_url': 'https://api.github.com/emojis', 'user_organizations_url': 'https://api.github.com/user/orgs', 'user_url': 'https://api.github.com/users/{user}', 'hub_url': 'https://api.github.com/hub', 'current_user_url': 'https://api.github.com/user', 'followers_url': 'https://api.github.com/user/followers', 'repository_url': 'https://api.github.com/repos/{owner}/{repo}', 'user_search_url': 'https://api.github.com/search/users?q={query}{&page,per_page,sort,order}', 'issues_url': 'https://api.github.com/issues'}

本文演示了GET方法及如何获取响应状态码、 响应头、 编码、 文本内容、 json内容。

最新文章

  1. --------------- Target-----------------
  2. Java Calendar 注意事项
  3. 1121冬至!!!巩固HTML基础第一堂
  4. Java正则表达式详解
  5. Shared libraries
  6. WebService相关概念介绍
  7. 解决Win7下运行php Composer出现SSL报错的问题
  8. [译]LINT TO SQL 介绍(数据库查询) - Part.3
  9. Windows操作系统常用快捷键
  10. hbase importtsv
  11. Android 解屏幕锁与点亮屏幕
  12. Wpf ScrollViewer with WrapPanel 使用鼠标滚轮水平滚动内容
  13. mysql show processlist
  14. weui 中的tabbar导航
  15. DNS服务器 知识点
  16. java基础知识—字符串
  17. POJ1083 Moving Tables
  18. LeetCode 100. Same Tree 判断两棵二叉树是否相等 C++
  19. 你应该要知道的vue.js
  20. js的一些总结

热门文章

  1. JavaScript 冒号(:)详解
  2. php调试时echo,print_r(),var_dump()的区别
  3. EMI (电磁干扰)
  4. (转)Oracle存储过程中的事务
  5. MinGW安装与环境变量配置和Sublime Text 2搭建C++编译环境
  6. 文件操作方法大全以及文件打开的其他一些模式sys.stdout.write()就是标准输出到你当前的屏幕 sys.stdout.flush()把内存立即显示到您当前的屏幕
  7. php根据年月获取当月天数。
  8. MFC 文件I/O和串行化
  9. Java微信公众平台开发【番外篇】(七)--公众平台测试帐号的申请
  10. aop计算方法耗时