python爬虫requests的使用
1 发送get请求获取页面
import requests # 1 要爬取的页面地址
url = 'http://www.baidu.com'
# 2 发送get请求 拿到响应
response = requests.get(url=url)
# 3 获取响应内容文本 两种方法
html1 = response.content.decode() #response.content为bytes类型,decode() 将它转换为utf8
print(html1) response.encoding='utf8'
html2 = response.text # 用response.text 会自动选择一种方式解码 有时候会乱码,要提前设置response.encoding
print(html2)
2 发送post请求获取页面
import requests # 1 要爬取的页面地址
url = 'http://www.baidu.com'
# 2 发送get请求 拿到响应
response = requests.post(url=url)
# 3 获取响应内容文本 两种方法
html1 = response.content.decode() #response.content为bytes类型,decode() 将它转换为utf8
print(html1) response.encoding='utf8'
html2 = response.text # 用response.text 会自动选择一种方式解码 有时候会乱码,要提前设置response.encoding
print(html2)
3 伪装浏览器,携带报头
import requests # 伪装我们的报文头,加上Use-Agent 伪装成浏览器
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
# 如果要带着cookie 可以传入cookie,也可以放在报文头当中
#'Cookie':'这里放入cookie'
}
# 1 要爬取的页面地址
url = 'http://www.baidu.com'
# 2 发送get请求 拿到响应
response = requests.get(url=url,headers=headers)
# 3 获取响应内容文本 两种方法
html = response.content.decode() #response.content为bytes类型,decode() 将它转换为utf8
print(html)
4 携带数据 (比如 发送请求去登陆)
import requests # 如果伪装登录,可以传送一个字典类型数据
data = {
'''这里放入需要的key:value'''
}
# 1 要爬取的页面地址
url = 'http://www.baidu.com'
# 2 发送get请求 拿到响应
# get请求用params 相当于在url后面拼接key=value&key=value
response = requests.get(url=url,params=data)
# post用data传入参数 携带post的数据
response = requests.post(url=url,data=data)
# 3 获取响应内容文本 两种方法
html = response.content.decode() #response.content为bytes类型,decode() 将它转换为utf8
print(html)
5 代理
import requests
# 将代理的服务器放入这里,key为协议类型 value为代理的ip和端口
# 发送https或者http请求会根据不同代理ip选择 为我们发送请求
proxies = {
'http':'http://127.0.0.1:80',
'https':'https://127.0.0.1:80'
} # 1 要爬取的页面地址
url = 'http://www.baidu.com'
# 2 发送get请求 拿到响应
response = requests.get(url=url,proxies=proxies)
# 3 获取响应内容文本 两种方法
html = response.content.decode() #response.content为bytes类型,decode() 将它转换为utf8
print(html)
6 携带cookie
import requests # 如果要带着cookie字典 可以传入cookie,也可以放在报文头当中
cookies = {
#'key':'value',
} # 或者将cookie放在报文头当中
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
# 如果要带着cookie 可以传入cookie,也可以放在报文头当中
#'Cookie':'这里放入cookie'
} # 1 要爬取的页面地址
url = 'http://www.baidu.com'
# 2 发送get请求 拿到响应
response = requests.get(url=url,cookies=cookies)
#response = requests.get(url=url,headers=headers)
# 3 获取响应内容文本 两种方法
html = response.content.decode() #response.content为bytes类型,decode() 将它转换为utf8
print(html)
7 保持session 帮我们保存response中的session
import requests
# 获取一个session对象为我们发送请求 用法与requests对象相同
session = requests.session() url = 'http://www.baidu.com'
#保持session发送请求
response = session.get(url=url)
# 获取页面
html = response.content.decode()
print(html)
#查看session
print(response.cookies)
8 设置连接超时时间
import requests
# 获取一个session对象为我们发送请求 用法与requests对象相同
session = requests.session() url = 'http://www.baidu.com'
#保持session发送请求
response = session.get(url=url,timeout = 3) # 3秒时间为超时时间
# 获取页面
html = response.content.decode()
print(html)
#查看session
print(response.cookies)
9 设置ssl校验 对方https协议合法性是否忽略
import requests
# 获取一个session对象为我们发送请求 用法与requests对象相同
session = requests.session() url = 'http://www.baidu.com'
#保持session发送请求
response = session.get(url=url,verify=False) # 不校验ssl 如果对方https协议不合法,我们忽略 继续请求
# 获取页面
html = response.content.decode()
print(html)
#查看session
print(response.cookies)
10 重新连接次数
import requests
from retrying import retry @retry(stop_max_attempt_number=3) # 设置超时重新连接 次数3
def get( url ):
response = requests.get(url=url,timeout=3)
return response.content.decode() url = 'http://www.baidu.com'
html = get(url)
print(html)
python爬虫requests的使用的更多相关文章
- Python爬虫—requests库get和post方法使用
目录 Python爬虫-requests库get和post方法使用 1. 安装requests库 2.requests.get()方法使用 3.requests.post()方法使用-构造formda ...
- Python 爬虫—— requests BeautifulSoup
本文记录下用来爬虫主要使用的两个库.第一个是requests,用这个库能很方便的下载网页,不用标准库里面各种urllib:第二个BeautifulSoup用来解析网页,不然自己用正则的话很烦. req ...
- Python爬虫--Requests库
Requests Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库,requests是python实现的最简单易用的HTTP库, ...
- 【Python成长之路】Python爬虫 --requests库爬取网站乱码(\xe4\xb8\xb0\xe5\xa)的解决方法【华为云分享】
[写在前面] 在用requests库对自己的CSDN个人博客(https://blog.csdn.net/yuzipeng)进行爬取时,发现乱码报错(\xe4\xb8\xb0\xe5\xaf\x8c\ ...
- Python爬虫 requests库基础
requests库简介 requests是使用Apache2 licensed 许可证的HTTP库. 用python编写. 比urllib2模块更简洁. Request支持HTTP连接保持和连接池,支 ...
- python 爬虫 requests+BeautifulSoup 爬取巨潮资讯公司概况代码实例
第一次写一个算是比较完整的爬虫,自我感觉极差啊,代码low,效率差,也没有保存到本地文件或者数据库,强行使用了一波多线程导致数据顺序发生了变化... 贴在这里,引以为戒吧. # -*- coding: ...
- python爬虫---requests库的用法
requests是python实现的简单易用的HTTP库,使用起来比urllib简洁很多 因为是第三方库,所以使用前需要cmd安装 pip install requests 安装完成后import一下 ...
- Python爬虫---requests库快速上手
一.requests库简介 requests是Python的一个HTTP相关的库 requests安装: pip install requests 二.GET请求 import requests # ...
- Python爬虫requests判断请求超时并重新发送请求
下面是简单的一个重复请求过程,更高级更简单的请移步本博客: https://www.cnblogs.com/fanjp666888/p/9796943.html 在爬虫的执行当中,总会遇到请求连接 ...
- python爬虫——requests库使用代理
在看这篇文章之前,需要大家掌握的知识技能: python基础 html基础 http状态码 让我们看看这篇文章中有哪些知识点: get方法 post方法 header参数,模拟用户 data参数,提交 ...
随机推荐
- 记录使用CI框架开发项目时遇到的问题
关于CI框架在视图文件中怎样引入静态资源文件(js,css,images)的问题: 第一步:在application/config/config.php文件中配置 $config['base_url ...
- python解决图的最短路径问题
在hihoCoder上遇到一个算法题目,描述如下: 对图结构有了解的不难发现,这是经典的求图的最短路径问题.以下是python代码: def findMin(row): minL = max(row) ...
- NGINX详解
目录 1. 基础概念 1 2. 版本选择 1 3. 服务安装 1 4. 模块说明 1 5. 配置说明 1 5.1 目录结构 1 ...
- Java中常见的URL问题及解决方案
URL无处不在,不过似乎开发人员并没有真正地理解它们,因为在Stack Overflow上经常看到有人在问如何正确的创建一个URL.想知道URL语法是如何工作的,可以看下兄弟连教育总结的这篇文章,非常 ...
- 项目中用到的node-express模块
反向代理中间件: var proxyMiddleWare = require("http-proxy-middleware"); var proxyPath = "htt ...
- 剑指Offer-对称的二叉树
package Tree; /** * 对称的二叉树 * 请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的. */ public class ...
- linux性能调试之vmstat
linux性能监控调优工具vmstat: vmstat:用于监控.显示系统运行过程中的虚拟内存/CPU/磁盘状态. 简单示例(时间间隔2s,监控2次): 重要字段解释: r 表示运行队列(等待运行的进 ...
- 移动web开发之rem的使用
为什么要使用rem 移动端设备尺寸五花八门,单纯使用px这个单位无法轻易适配,rem就可以为我们解决这个问题! 如何使用rem 1rem默认等于16px,这是因为页面的默认字体大小就是16px.r 代 ...
- linux --> VIM的列编辑操作
VIM的列编辑操作 一.删除列 1.光标定位到要操作的地方. 2.CTRL+v 进入“可视 块”模式,选取这一列操作多少行. 3.d 删除. 二.插入列 插入操作的话知识稍有区别.例如在每一行 ...
- .net core2.0下Ioc容器Autofac使用
.net core发布有一段时间了,最近两个月开始使用.net core2.0开发项目,大大小小遇到了一些问题.准备写个系列介绍一下是如何解决这些问题以及对应技术.先从IOC容器Autofac开始该系 ...