Python模块之Requests
Requests 模块
requests是在python中用于制造HTTP标准请求(request)头的模块,即模拟浏览器请求,它抽象了一个在美丽、简单的API背后发出请求的复杂性,即降低了HTTP请求头制造的复杂性
这样,以便您可以专注于与服务交互和在应用程序中使用数据
为何要使用requests模块
1、自动处理url编码
2、自动处理post请求参数
3、简化cookie代理的实现
request模块使用流程
1、指定url
2、使用requests模块发起请求
3、获取响应数据
4、进行持久化存储
requests模块安装:
pip install requests
requests模块导入:
import requests
常规的get请求
1、常规的get请求
需求:爬取搜狗首页的页面数据
import requests
# 指定url
url = "https://www.sogou.com/"
# 发起get请求:get方法会返回请求成功后的响应对象
response = requests.get(url=url)
# 获取响应中的数据值:.text可以获取响应对象中字符串形式的页面数据我、
page_data = response.text
print(page_data)
# 持久化操作
with open('./sogou.html','w',encoding='utf-8') as f:
f.write(page_data)
response对象中其它重要的属性
# content:获取的是response对象中二进制(byte)类型的页面数据
print(response.content)
# status_code:返回一个响应状态码
print(response.status_code)
# headers:返回响应头信息
print(response.headers)
# url:获取请求的url
print(response.url)
2、携带参数的get请求
需求:指定一个词条,获取搜狗搜索结果所对应的页面数据
方式一:
import requests
# 指定url
url = "https://www.sogou.com/web?query=纽约&ie=utf-8"
response = requests.get(url=url)
page_text = response.text
with open('./niuyue.html','w',encoding='utf-8') as f:
f.write(page_text)
方式二:
import requests
# 指定url
url = "https://www.sogou.com/web"
# 将参数封装到字典中
params = {
'query':'纽约',
'ie':'utf-8',
}
response = requests.get(url=url,params=params)
page_text = response.text
3、自定义请求头信息
自定义GET请求的一种常用方法是通过URL中的查询字符串参数传递值。需要用get()执行此操作,你可以将自定义数据传递给params方法。
url地址格式如下图:

import requests
# 指定url
url = "https://www.sogou.com/web"
# 将参数封装到字典中
params = {
'query':'纽约',
'ie':'utf-8',
}
# 自定义请求头信息
headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
}
response = requests.get(url=url,params=params,headers=headers)
page_text = response.text
print(page_text)
基于ajax的get请求
基于ajax的get请求,实际调用的方法还是requests.get
需求:抓取豆瓣电影上电影详情的数据
import requests
url = "https://movie.douban.com/j/chart/top_list?"
# 封装ajax的get请求中携带的参数
params = {
'type': '17',
'interval_id': '100:90',
'action':'',
'start': '0',
'limit': '200',
}
# 自定义请求头信息
headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
}
response = requests.get(url=url,params=params,headers=headers)
print(response.text)
常规的post请求
1、常规的post请求
需求:登录豆瓣网,获取登录成功后的页面数据
import requests
# 指定post请求的url
url = 'https://accounts.douban.com/j/mobile/login/basic'
# 封装post请求的参数
data={
'ck': 'vMY5',
'name': 'jasonminghao@163.com',
'password': 'xmhdb1213',
'remember': 'false',
}
# 自定义请求头信息
headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
}
# 发起post请求
response = requests.post(url=url,data=data,headers=headers)
# 获取响应对象中的页面数据
page_text = response.text
print(page_text)
基于ajax的post请求
需求:爬取肯德基城市餐厅位置数据
import requests
url = "http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword"
# 处理post请求的参数
data = {
'cname': '',
'pid': '',
'keyword': '广州',
'pageIndex': '1',
'pageSize': '10',
}
# 自定义请求头信息
headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
}
# 发起基于ajax的post请求
response = requests.post(url=url,data=data,headers=headers)
print(response.text)
综合项目实战
需求:爬取搜狗知乎某一个词条对应一定范围页码表示的页面数据
# 前三页面数据(1,2,3)
import requests
import os
# 创建一个文件夹
if not os.path.exists('./pages'):
os.mkdir('./pages')
word = input('enter a word:')
# 动态指定页码的范围
start_pageNum = int(input('enter a start page number:'))
end_pageNum = int(input('enter a end page number:'))
# 指定url: 设计成一个具有通用的url
url = "https://www.sogou.com/sogou"
# 自定义请求头信息
headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
}
for page in range(start_pageNum,end_pageNum+1):
param = {
'query':word,
'page':page,
'id':'utf-8',
}
response = requests.get(url=url,params=param,headers=headers)
# 获取响应中的页面数据(指定页码)
page_text = response.text
fileNmae = word+ "_" + str(page) + '.html'
filePaath = './pages/' + fileNmae
# 进行持久化存储
with open(filePaath,'w',encoding='utf-8') as f:
f.write(page_text)
print('第%s页数据写入成功' %page)
requests模块高级
cookie基于用户的用户数据,当我们在某个网站里登录输入账号密码登录的时候,服务端会发送cookie给客户端,
cookie:服务器端使用cookie来记录客户端的状态信息
实现流程:
1、执行登录操作(获取cookie)
2、在发起个人主页请求时,需要将cookie携带到该请求中
注意:session对象:发送请求(会将cookie对象自动存储)
import requests
session = requests.session()
#1、发起登录请求:将cookie获取,并存储到session
login_url = 'https://accounts.douban.com/j/mobile/login/basic'
# 封装post请求的参数
data={
'ck': 'vMY5',
'name': '',
'password': '',
'remember': 'false',
}
# 自定义请求头信息
headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
}
# 使用session发起post请求
login_response = session.post(url=login_url,data=data,headers=headers)
cookie = login_response.cookies
# 2、对个人主页发起请求(session),获取响应页面数据
url = "https://www.douban.com/people/jason12313/"
response = session.get(url=url,headers=headers)
page_text = response.text
with open('./douban110.html','w',encoding='utf-8') as f:
f.write(page_text)
requests代理
代理,称为第三方代理本体执行相关的事务,生活:代购,微商,房屋中介,
为何要使用代理?
很多时候我们去爬别人的网站时,如果爬的频率太高,例如一秒1000次请求,就会导致当前源IP地址被目标网站给禁止,那么我们就可以使用代理,让代理来作为源IP来执行爬虫。
代理分类
1、正向代理:代理客户端获取数据
2、反向代理:代理服务器端提供数据
免费代理提供商
- www.goubanjia.com
- 快代理
- 西祠代理
import requests
url = 'http://www.baidu.com/s?ie=utf-8&wd=ip'
# 将代理IP封装到字典中
proxy = {
'http':'183.146.213.198:80',
}
headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
}
# 更换网络IP
response = requests.get(url=url,proxies=proxy,headers=headers)
print(response.text)
with open('./daili.html','w',encoding='utf-8') as f:
f.write(response.text)
验证码处理
- 手动识别验证码
- 云打码平台自动识别验证码
云打码平台处理验证码的实现流程:
1、对携带验证码的页面数据进行抓取
2、可以将页面数据中验证码进行解析,验证码图片下载到本地
3、可以将验证码图片提交给三方平台进行识别,返回验证码图片上的数据值
Python模块之Requests的更多相关文章
- windows安装Python模块:requests
个人在windows10安装python模块requests如下过程: 1.下载requests模块:首先打开powershell, cd到你要下载文件的位置(我的是d:\softwareinstal ...
- Python模块之requests,urllib和re
目录 一.爬虫的步骤 二.使用Jupyter 三.爬虫请求模块之urllib 四.爬虫请求模块之requests 五.爬虫分析之re模块 一.爬虫的步骤 1.发起请求,模拟浏览器发送一个http请求 ...
- python模块(requests,logging)
一.requests Requests 是使用 Apache2 Licensed 许可证的 基于Python开发的HTTP 库,其在Python内置模块的基础上进行了高度的封装,从而使得Pythone ...
- python模块中requests参数stream
PS:这个参数真没用过 当下载大的文件的时候,建议使用strea模式. 默认情况下是false,他会立即开始下载文件并存放到内存当中,倘若文件过大就会导致内存不足的情况. 当把get函数的stream ...
- Python高手之路【八】python基础之requests模块
1.Requests模块说明 Requests 是使用 Apache2 Licensed 许可证的 HTTP 库.用 Python 编写,真正的为人类着想. Python 标准库中的 urllib2 ...
- python 网络爬虫requests模块
一.requests模块 requests模块是python中原生的基于网络请求的模块,其主要作用是用来模拟浏览器发起请求.功能强大,用法简洁高效. 1.1 模块介绍及请求过程 requests模块模 ...
- 孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块
孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块 (完整学习过程屏幕记录视频地址在文末) 从今天起开始正式学习Python的爬虫. 今天已经初步了解了两个主要的模块: ...
- python网络编程----requests模块
python访问网站可以用标准模块--urllib模块(这里省略),和requests(安装-pip install requests)模块,requests模块是在urllib的基础上进行的封装,比 ...
- Python爬虫练习(requests模块)
Python爬虫练习(requests模块) 关注公众号"轻松学编程"了解更多. 一.使用正则表达式解析页面和提取数据 1.爬取动态数据(js格式) 爬取http://fund.e ...
随机推荐
- MVC 记录
ASP.NET MVC框架提供了一个帮助我们构造Html元素的类:TagBuilder ps url cnblogs.com/yibinboy/articles/5187682.html HttpRu ...
- mongo 查询 距离 某个点 多少 米距离 感谢 提供的数据。 感谢 mvc的 demo 。反正 就是各种感谢 文档之类的。
昨天 去面试来着, 问了一下mong . 我记得mong支持 地理位置索引的,说了一下. 然后 面试官说 查询某个点 的 多少米范围, 这个该怎么实现? 我懵逼了.... 回去 查询了一下. 发现有 ...
- 创建自定义的RouteBase实现(Creating a Custom RouteBase Implementation) |定制路由系统 |
- nuxt.js学习初探
项目目标 把我个人博客的前端界面部分使用nuxt框架进行服务端渲染 nuxt介绍 nuxt可以把spa根据路由将单页面分割成多页面,比起vue的ssr渲染要更容易使用 nuxt的使用 项目创建 npx ...
- JS-05-元素获取
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- js----UTC时间于本地时间相差8小时问题
js----UTC时间于本地时间相差8小时问题 js获取周几有两个方法getDay() getUTCDay(),但是它们是有区别的,前者返回的本地时间,后者返回的UTC时间,一般情况下,两者相差8个小 ...
- Linux下的expect
expect简介 expect是一款自动化的脚本解释型的工具. expect基于tcl脚本,expect脚本的运行需要tcl的支持. expect对一些需要交互输入的命令很有帮助,比如ssh ftp ...
- Leetcode 题目整理-8 Count and Say
38. Count and Say The count-and-say sequence is the sequence of integers beginning as follows: 1, 11 ...
- Mybatis基础(二)
Mybatis连接池 Mybatis连接池提供了三种配置方式,配置的位置在SqlMapConfig.xml的dataSource标签中,其type属性就是配置连接池的种类.type的可取值 1.POO ...
- Nacos数据模型
Nacos 数据模型 Key 由三元组唯一确定, Namespace默认是空串,公共命名空间(public),分组默认是 DEFAULT_GROUP. 以上都是nacos官网上面的图片及描述,综合一下 ...