requests

实例引入

import requests

response  = requests.get('https://www.baidu.com')
response.status_code
response.text
response.cookies

请求方式

post()
put()
delete()
head()
options()

请求

基本get请求

带参数get请求

data = {'name':'germey', 'age':'22}
response = request.get('http://httpbin.org/get', params=data)
print(respones.text)

解析json

response.json()

获取二进制数据

response.content
response=request.get('https://github.com/favicon.ico')
f = open('favicon.ico', 'wb')
f.write(response.content)
f.close()

添加headers

headers={
'User-Agent':'Mozilla/5.0 (Macintosh; intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'
}
response = request.get('https://www.zhihu.com/explore'. headers=headers_

POST请求

data = {}
headers = {}
response = request.post('http://httpbin.org/post', data=data, headers=headers)

response属性

status_code
headers
cookies
url
history

高级操作

文件上传

files = {'file':open('favicon.ico','rb')}
response = request.get('http://httpbin.org/post', files=files)

获取cookie

for key,value in response.cookies.items():
print(key + '=' + value)

会话维持

requests.get('http://httpbin.org/cookies/set/number/123456789)
response = requests.get('http://httpbin.org/cookies')

上述方法无法得到想要的cookie

s = requests.Session()
s.get(...)
response = s.get(...)

证书验证

暂时不看。如果发生情况则添加参数 verify=False

代理设置

proxies={}
response = requests.get(' ', proxies=proxies)

超时设置

from requests.exceptions import ReadTimeout

try:
#some codes
except ReadTimeout:
print('Timeout')

认证设置

request.get(...,auth={'user','123'})

异常处理

爬虫二之Requests的更多相关文章

  1. Python 爬虫二 requests模块

    requests模块 Requests模块 get方法请求 整体演示一下: import requests response = requests.get("https://www.baid ...

  2. 爬虫二 requests模块的使用

    一.requests模块的介绍 #介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3) #注意:reques ...

  3. 爬虫学习(二)requests模块的使用

    一.requests的概述 requests模块是用于发送网络请求,返回响应数据.底层实现是urllib,而且简单易用,在python2.python3中通用,能够自动帮助我们解压(gzip压缩的等) ...

  4. 爬虫四大金刚:requests,selenium,BeautifulSoup,Scrapy

    一.简介爬虫 1.什么是爬虫 #1.什么是互联网? 互联网是由网络设备(网线,路由器,交换机,防火墙等等)和一台台计算机连接而成,像一张网一样. #2.互联网建立的目的? 互联网的核心价值在于数据的共 ...

  5. 【python网络爬虫】之requests相关模块

    python网络爬虫的学习第一步 [python网络爬虫]之0 爬虫与反扒 [python网络爬虫]之一 简单介绍 [python网络爬虫]之二 python uillib库 [python网络爬虫] ...

  6. 爬虫开发5.requests模块的cookie和代理操作

    代理和cookie操作 一.基于requests模块的cookie操作 引言:有些时候,我们在使用爬虫程序去爬取一些用户相关信息的数据(爬取张三“人人网”个人主页数据)时,如果使用之前requests ...

  7. 爬虫系列(七) requests的基本使用

    一.requests 简介 requests 是一个功能强大.简单易用的 HTTP 请求库,可以使用 pip install requests 命令进行安装 下面我们将会介绍 requests 中常用 ...

  8. 爬虫中之Requests 模块的进阶

    requests进阶内容 session处理cookie proxies参数设置请求代理ip 基于线程池的数据爬取 引入 有些时候,我们在使用爬虫程序去爬取一些用户相关信息的数据(爬取张三“人人网”个 ...

  9. 网络爬虫入门:你的第一个爬虫项目(requests库)

    0.采用requests库 虽然urllib库应用也很广泛,而且作为Python自带的库无需安装,但是大部分的现在python爬虫都应用requests库来处理复杂的http请求.requests库语 ...

随机推荐

  1. 第二篇:请求库之requests,selenium

    requests模块 一.介绍 #介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3) #注意:reques ...

  2. C标签的用法

    今天做jsp页面,发现无法将后台传过来的时间戳转换成正常的时间格式,于是就想到了C标签,顺便把C标签总结一下 1.引入C标签 <%@ taglib uri="http://java.s ...

  3. 如何导出不带.svn的文件夹

    在工作环境中,有的时候需要将本地SVN服务器中的文件导出来,提交到另一个SVN服务器中去(比如做现场开发时,由于外网速度慢,项目组内部往往使用一个SVN服务器,但又同时又需要公司统一管理,定期提交到公 ...

  4. Shell-07数组与字符串

    Shell-07数组与字符串 数组 数组说白了就是一段连续的变量,一段连续的内存存储空间 解决:变量过多的问题:在同类的变量中,我们不需要去定义多个名字,而是以数组的方式来定义:(列表) 数组名 索引 ...

  5. jsp+servlet怎么实现文件断点上传下载

    我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 这次项目的需求: 支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,i ...

  6. HDU 1097 快速幂

    #include<iostream> using namespace std; long long quick(long long a,long long b,int c) { ; a=a ...

  7. 【bzoj3672&&uoj7】[Noi2014]购票

    *题目描述: 今年夏天,NOI在SZ市迎来了她30周岁的生日.来自全国 n 个城市的OIer们都会从各地出发,到SZ市参加这次盛会. 全国的城市构成了一棵以SZ市为根的有根树,每个城市与它的父亲用道路 ...

  8. VirtualBox NAT Host-only模式下,自动分配IP上网。

    修改宿主机上,virtualbox自建虚拟网卡Host-Only 2. 因为我使用了两个适配器,所以这两个适配器的名字分别是ifcfg-eth0, ifcfg-eth1. ifcfg-eh0一般默认就 ...

  9. Bootstrap Table--onEditableSave

    当某列编辑完成后,需要对当前列所在的行进行修改操作: $("#grid").bootstrapTable({ url:'', …… …… //其他属性 columns:[{ fie ...

  10. mysql中对比 JSON_VALUE 与 JSON_QUERY

    1. JSON概述 MySQL里的json分为json array和json object. $表示整个json对象,在索引数据时用下标(对于json array,从0开始)或键值(对于json ob ...