python Requests库总结

什么是Requests库?

requests库github地址:https://github.com/requests/requests

Reqyests库主要用来准备Request和处理Response。

为什么要学习Requests库?

web开发和爬虫都需要学习的东西,在服务端编程中理解好Requests库可以更好的编写Restful API的程序,还是自动化测试的工具箱。

安装Requests库

pip install requests

这个是安装requests库的

pip install gunicorn

gunicorn是一个python Wsgi http server,只支持在Unix系统上运行,来源于Ruby的unicorn项目。

pip install httpbin

httpbin是一个http库的测试工具

gunicorn httpbin:app

通过gunicorn启动httpbin,可以通过127.0.0.1/8000访问

简单了解http协议

http协议:HyperText Transfer Protocl 超文本传输协议.

http协议是应用层上的一个无状态的协议,是一种为分布式,协作式,多媒体信息服务的协议。

> GET / HTTP/1.1
> Host: www.imooc.com
> User-Agent: curl/7.47.0
> Accept: */*

Request:

第一行:分别是方法:GET,地址:/,协议:HTTP/1.1。

二三四行以key:value的形式组成headers。

< HTTP/1.1 200 OK
< Server: nginx
< Date: Sun, 16 Sep 2018 14:36:46 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 249314
< Connection: keep-alive
< Vary: Accept-Encoding
< Vary: Accept-Encoding
< X-Varnish: 636943726 641514266
< Age: 20
< Via: 1.1 varnish (Varnish/6.0)
< X-Cache: HIT from CS42
< Accept-Ranges: bytes

Response:

start line:状态码,具体解释

后面的也是组成一个headers,告诉浏览器怎么具体解析

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>慕课网-程序员的梦工厂</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
<meta name="renderer" content="webkit" />
<meta name="mobile-agent" content="format=wml"; url="https://m.imooc.com/">
<link rel="alternate" media="only screen and (max-width: 640px)" href="https://m.imooc.com/">
<meta name="mobile-agent" content="format=xhtml"; url="https://m.imooc.com/">
<meta name="mobile-agent" content="format=html5"; url="https://m.imooc.com/">
<meta property="qc:admins" content="77103107776157736375" />
<meta property="wb:webmaster" content="c4f857219bfae3cb" />
<meta http-equiv="Access-Control-Allow-Origin" content="*" />
<meta http-equiv="Cache-Control" content="no-transform " />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link rel="dns-prefetch" href="//www.imooc.com" />
<link rel="dns-prefetch" href="//img.imooc.com" />
<link rel="dns-prefetch" href="//img.mukewang.com" />
<link rel="apple-touch-icon" sizes="76x76" href="/static/img/common/touch-icon-ipad.png">
<link rel="apple-touch-icon" sizes="120x120" href="/static/img/common/touch-icon-iphone-retina.png">
<link rel="apple-touch-icon" sizes="152x152" href="/static/img/common/touch-icon-ipad-retina.png">
<meta name="Keywords" content="" />
<meta name="Description" content="慕课网(IMOOC)是IT技能学习平台。慕课网(IMOOC)提供了丰富的移动端开发、php开发、web前端、android开发以及html5等视频教程资源公开课。并且富有交互性及趣味性,你还可以和朋友一起编程。" />

Message Body

原文地址:https://www.cnblogs.com/shhyzzu/p/9657971.html

时间: 2024-07-30 21:32:29

python Requests库总结的相关文章

大概看了一天python request源码。写下python requests库发送 get,post请求大概过程。

python requests库发送请求时,比如get请求,大概过程. 一.发起get请求过程:调用requests.get(url,**kwargs)-->request('get', url, **kwargs)-->session.request(method="get", url=url, **kwargs)-->session.send(request, **kwargs)-->adapter.send(request, **kwargs)-->

python requests库学习笔记(上)

尊重博客园原创精神,请勿转载! requests库官方使用手册地址:http://www.python-requests.org/en/master/:中文使用手册地址:http://cn.python-requests.org/zh_CN/latest/: requests库作者Kenneth Reitz个人主页:https://www.kennethreitz.org/: requests库github地址:https://github.com/requests/requests: requ

Python:requests库、BeautifulSoup4库的基本使用(实现简单的网络爬虫)

Python:requests库.BeautifulSoup4库的基本使用(实现简单的网络爬虫) 一.requests库的基本使用 requests是python语言编写的简单易用的HTTP库,使用起来比urllib更加简洁方便. requests是第三方库,使用前需要通过pip安装. pip install requests 1.基本用法: import requests #以百度首页为例 response = requests.get('http://www.baidu.com') #res

Python Requests库:HTTP for Humans

Python标准库中用来处理HTTP的模块是urllib2,不过其中的API太零碎了,requests是更简单更人性化的第三方库. 用pip下载: pip install requests 或者git: git clone git://github.com/kennethreitz/requests.git 发送请求: GET方法 >>> import requests >>> r = requests.get('https://api.github.com/event

python requests库学习笔记(下)

1.请求异常处理 请求异常类型: 请求超时处理(timeout): 实现代码: import requestsfrom requests import exceptions        #引入exceptions A:请求超时 def timeout_request():    try:        response = requests.get(build_uri('user/emails'), timeout=0.1)    except exceptions.Timeout as e:

python requests 库 首次使用

安装requests库 执行pip3 install requests 使用resquests库获取百度网站首页 打开python idle终端.以python3为例,在终端执行python3并回车. 输入 import requests , 表示引用requests库. 输入 r = requests.get("http://www.baidu.com"), 表示以get方式获取百度网页. r.status_code 返回200,表示返回成功. r.encoding='utf-8'

python Requests库入门

一.Requests 库的安装 对Requests 库 更多了解移步:http://www.python-requests.org 安装操作: win平台:"以管理员身份运行" cmd ,执行 pip install requests 测试安装是否成功: >>> import requests >>> r=requests.get('http://www.baidu.com') >>> print(r.status_code) 20

python requests库入门[转]

首先,确认一下: Requests 已安装 Requests是 最新的 让我们从一些简单的示例开始吧. 发送请求 使用Requests发送网络请求非常简单. 一开始要导入Requests模块: >>> import requests 然后,尝试获取某个网页.本例子中,我们来获取Github的公共时间线 >>> r = requests.get('https://github.com/timeline.json') 现在,我们有一个名为 r 的 Response 对象.可

python requests库使用

迫不及待了吗?本页内容为如何入门 Requests 提供了很好的指引.其假设你已经安装了 Requests.如果还没有,去安装一节看看吧. 首先,确认一下: Requests 已安装 Requests 是最新的 让我们从一些简单的示例开始吧. 发送请求 使用 Requests 发送网络请求非常简单. 一开始要导入 Requests 模块: >>> import requests 然后,尝试获取某个网页.本例子中,我们来获取 Github 的公共时间线: >>> r =