5.爬虫 requests库讲解 高级用法
0.文件上传
import requests
files = {'file': open('favicon.ico', 'rb')}
response = requests.post("http://httpbin.org/post", files=files)
print(response.text)
1.获取cookies
import requests
response = requests.get("https://www.baidu.com")
print(response.cookies)
for key, value in response.cookies.items():
print(key + '=' + value)
2.会话维持
import requests
requests.get('http://httpbin.org/cookies/set/number/123456789')
response = requests.get('http://httpbin.org/cookies')
print(response.text)
*可以通过http://httpbin.org/cookies/set/number/123456789对这个网址设置个cookies
输出结果如下:
{
"cookies": {}
}
为空?!因为两次get请求,实际上相当于你用两个浏览器打开了不同的网页。用Session()方法试试?
import requests s = requests.Session()
s.get('http://httpbin.org/cookies/set/number/123456789')
response = s.get('http://httpbin.org/cookies')
print(response.text)
输出结果如下:
{
"cookies": {
"number": "123456789"
}
}
* 用Session()我们实现了维持会话登陆模拟登陆(即用于模拟在一个浏览器中打开同一站点的不同页面)
3.证书验证
import requests
response = requests.get('https://www.12306.cn')
print(response.status_code)
# 提示出现SSLError表示证书验证错误
#######################
#去除警告
import requests
from requests.packages import urllib3
urllib3.disable_warnings()
response = requests.get('https://www.12306.cn', verify=False)
print(response.status_code)
#######################
#指定一个本地证书用作客户端证书
import requests
response = requests.get('https://www.12306.cn', cert=('/path/server.crt', '/path/key'))
print(respo
nse.status_code)
4.代理设置
#无密码的
import requests proxies = {
"http": "http://127.0.0.1:9743",
"https": "https://127.0.0.1:9743",
} response = requests.get("https://www.taobao.com", proxies=proxies)
print(response.status_code) ############################## #有密码的
import requests proxies = {
"http": "http://user:password@127.0.0.1:9743/",
}
response = requests.get("https://www.taobao.com", proxies=proxies)
print(response.status_code) ############################## #代理不支持http,支持sockes
#pip3 install 'requests[socks]'
import requests proxies = {
'http': 'socks5://127.0.0.1:9742',
'https': 'socks5://127.0.0.1:9742'
}
response = requests.get("https://www.taobao.com", proxies=proxies)
print(response.status_code)
5.超时设置
import requests
from requests.exceptions import ReadTimeout
try:
response = requests.get("http://httpbin.org/get", timeout = 0.5)
print(response.status_code)
except ReadTimeout:
print('Timeout')
*timeout = (5,30) 5是连接超时时间 30是读取超时时间
*timeout = 35 35是连接和读取两者之和
*timeout = None 或者我不设置 代表永久等待
6.认证设置
import requests
from requests.auth import HTTPBasicAuth r = requests.get('http://120.27.34.24:9001', auth=HTTPBasicAuth('user', ''))
#还可以像下面这样写 简单些(默认使用HTTPBasicAuth这个类来认证 当然这个网址访问不了的)
#r = requests.get('http://120.27.34.24:9001', auth=('user', '123'))
print(r.status_code)
7.异常处理
import requests
from requests import ReadTimeout, ConnectionError, RequestException
try:
response = requests.get("http://httpbin.org/get", timeout = 0.5)
print(response.status_code)
except ReadTimeout:
print('Timeout')
except ConnectionError:
print('Connection error')
except RequestException:
print('Error')
*可以去requests库的官方文档,找到API,再看里面的异常!!
8.Prepared Request
*在urllib里,可以将请求表示为数据结构,其余各个参数都可以通过一个Request对象来表示.
*在requests里,用Prepared Request同样可以做到!
from requests import Request,Session
url = "..."
data = {'...':'...'}
headers = {'User-Agent':'...'}
s = Session()
req = Request('POST',url,data = data,headers = headers)
prepped = s.prepare_request(req)
r = s.send(prepped)
print(r.text)
*在这里,我们引入Request,然后用url、data、headers参数构造了一个Requests对象,这时候调用Session的prepare_request()方法将其转换为一个Prepared Request对象,然后再调用send方法发送即可。
*有了这个Requests对象,就可以将请求当作独立的对象来看待,这样在进行队列调度时会非常方便。
5.爬虫 requests库讲解 高级用法的更多相关文章
- Python爬虫Urllib库的高级用法
Python爬虫Urllib库的高级用法 设置Headers 有些网站不会同意程序直接用上面的方式进行访问,如果识别有问题,那么站点根本不会响应,所以为了完全模拟浏览器的工作,我们需要设置一些Head ...
- Python中第三方库Requests库的高级用法详解
Python中第三方库Requests库的高级用法详解 虽然Python的标准库中urllib2模块已经包含了平常我们使用的大多数功能,但是它的API使用起来让人实在感觉不好.它已经不适合现在的时代, ...
- 爬虫requests库的基本用法
需要注意的几个点: 1.后面的s是一个虚拟目录 2.url后面不用加问号,发起请求的时候会自动帮你加上问号 get_url = 'http://www.baidu.com/s' 3. url的特性:u ...
- 4.爬虫 requests库讲解 GET请求 POST请求 响应
requests库相比于urllib库更好用!!! 0.各种请求方式 import requests requests.post('http://httpbin.org/post') requests ...
- 6.爬虫 requests库讲解 总结
requests库的总结: 用ProcessOn根据前面的几节内容做了个思维导图:
- Python爬虫 Urllib库的高级用法
1.设置Headers 有些网站不会同意程序直接用上面的方式进行访问,如果识别有问题,那么站点根本不会响应,所以为了完全模拟浏览器的工作,我们需要设置一些Headers 的属性. 首先,打开我们的浏览 ...
- Python爬虫入门之Urllib库的高级用法
1.设置Headers 有些网站不会同意程序直接用上面的方式进行访问,如果识别有问题,那么站点根本不会响应,所以为了完全模拟浏览器的工作,我们需要设置一些Headers 的属性. 首先,打开我们的浏览 ...
- Python爬虫入门四之Urllib库的高级用法
1.设置Headers 有些网站不会同意程序直接用上面的方式进行访问,如果识别有问题,那么站点根本不会响应,所以为了完全模拟浏览器的工作,我们需要设置一些Headers 的属性. 首先,打开我们的浏览 ...
- 转 Python爬虫入门四之Urllib库的高级用法
静觅 » Python爬虫入门四之Urllib库的高级用法 1.设置Headers 有些网站不会同意程序直接用上面的方式进行访问,如果识别有问题,那么站点根本不会响应,所以为了完全模拟浏览器的工作,我 ...
随机推荐
- 解决 Your project contains error(s),please fix them before running your applica ..
解决 Your project contains error(s),please fix them before running your application问题 http://www.cnblo ...
- 复合词(Compound Words, UVa 10391)(stl set)
You are to find all the two-word compound words in a dictionary. A two-word compound word is a word i ...
- HDU 1159 Common Subsequence(裸LCS)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...
- GOPL第三章练习题3.3 3.4代码
练习3.3是peak展示为红色,valley展示为蓝色. 练习3.4是将svg图像打印到浏览器中. // Copyright © 2016 Alan A. A. Donovan & Brian ...
- Go转json数组
Go转json数组 最近因需要要调用gitlab的API,其中有一个是根据私有token获取Repositories列表 由于返回结果是一个json数组,单纯使用json.Unmarshal没法实现, ...
- python学习笔记--数据类型
Life is short, You need Python! 霸气的口号! 今天我也开始学python了,毕竟不懂后端的前端不是好前端.之前有过‘世界上最好的语言’和JavaScript的学习经验. ...
- C++最接近整数的浮点运算
Function return ceil 不小于给定值的最接近整数值 floor 不大于给定值的最接近整数 trunc (C++11) 绝对值不大于给定值的最接近整数 round(C++11) 最接近 ...
- chrome浏览器中 F12 功能的简单介绍
chrome浏览器中 F12 功能的简单介绍 由于F12是前端开发人员的利器,所以我自己也在不断摸索中,查看一些博客和资料后,自己总结了一下来帮助自己理解和记忆,也希望能帮到有需要的小伙伴,嘿嘿! 首 ...
- vue js 保留小数
toFixed(number,fractionDigits ){ //number 保留小数数 //fractionDigits 保留小数位数 var times = Math.pow(10, fra ...
- HTML中的【块】与【内嵌】
块元素与内嵌元素 块的特征 默认独占一行 没有宽度时默认撑满一行 支持所有的css命令 内嵌的特征 同行可以连续跟同类的标签 内容撑开宽度 不支持宽高 不支持上下的内外边距 代码换行被解析 块与内嵌的 ...