一、request模块介绍

1. 什么是request模块

   - python中原生的基于网络请求的模块,模拟浏览器发起请求。

2. 为什么使用request模块

   - urllib需要手动处理url编码,quote()。
- urllib需要手动处理post请求参数。
- cookie的代理操作比较繁琐
1. cookie
- 创建一个cookiejar对象
- 创建一个handler对象
- 创建一个openner
2. 代理
- 创建handler对象,代理ip和端口分装到该对象
- 创建openner对象

3. request如何被使用

   - 安装:pip install requests
- 使用流程:
1. 指定URL
2. 使用request模块发起请求
3. 获取响应数据
4. 进行持久化储存

3.通过5个基于request模块的爬虫项目对该模块进行系统学习和巩固

   - get请求
- post请求
- 基于ajax的get请求
- 基于ajax的post请求
- 综合项目

二、项目实战

3. 基于request模块发起一个get请求

需求:爬取搜狗首页的页面数据

import requests

# 指定url
url = 'https://www.sogou.com/' # 发起get请求:get方法会返回请求成功的响应对象
response = requests.get(url=url) # 获取响应中的数据值:text可以获取响应对象中字符串形式的页面数据
page_data = response.text # 持久化操作
with open('./sougou.html','w',encoding='utf-8') as fp:
fp.write(page_data)

response对象中其他重要的属性

import requests

# 指定url
url = 'https://www.sogou.com/' # 发起get请求:get方法会返回请求成功的响应对象
response = requests.get(url=url) # content获取的是response对象中二进制(byte)类型的页面数据
# print(response.content) # 返回一个响应状态码
# print(response.status_code) # 响应头信息
# print(response.headers) # 获取请求的url
# print(response.url)

4. 携带参数的get请求方式1

import requests

url = 'https://www.sogou.com/web?query=周杰伦&ie=utf-8'

response = requests.get(url=url)

page_text = response.text

with open('./zhou.html','w',encoding='utf-8') as fp:
fp.write(page_text)

5. 携带参数的get请求方式2

import requests

url = 'https://www.sogou.com/web'

# 将参数封装到字典中
params = {
'query':'周杰伦',
'ie':'utf-8',
} requests.get(url=url,params=params) print(response.text)

6. 自定义请求头信息

import requests

url = 'https://www.sogou.com/web'

# 将参数封装到字典中
params = {
'query':'周杰伦',
'ie':'utf-8',
} # 自定义请求头信息
headers = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
} response = requests.get(url=url,params=params,headers=headers) print(response.status_code)

7. 基于requests模块发起的post请求

需求:登陆豆瓣网,获取登陆成功后的页面数据

import requests

# 1. 指定post请求的url
url = 'https://accounts.douban.com/login' # 封装post请求的参数
data = {
'source':'movie',
'redir':'https://movie.douban.com/',
'form_email':'account',
'form_password':'password',
'login':'登陆',
} # 自定义请求头信息
headers = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
} # 2. 发起post请求
response = requests.post(url=url,data=data,headers=headers) # 3. 获取响应对象中的页面数据
page_text = response.text # 4. 持久化操作
with open('./douban.html','w',encoding='utf-8') as fp:
fp.write(page_text)

8. 基于Ajax的get请求

需求:抓取豆瓣电影上电影详情的数据

import requests

url =  'https://movie.douban.com/j/chart/top_list?'

# 封装ajax的get请求中携带的参数(系统自带的抓包工具下面的Query String)
params = {
'type': '',
'interval_id': '100:90',
'action':'',
'start': '',
'limit': '',
} # 自定义请求头信息
headers = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
} response = requests.get(url=url,params=params,headers=headers) print(response.text)

9. 基于Ajax的post请求

需求:爬取肯德基城市餐厅位置数据

import requests

# 1. 指定url
post_url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword' # 处理post请求
data = {
'cname': '',
'pid': '',
'keyword': '北京',
'pageIndex': '',
'pageSize': '',
} # 自定义请求头信息
headers = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
} # 2. 发起基于ajax的post请求
response = requests.post(url=post_url,headers=headers,data=data) print(response.text)

总结:普通的get和post的请求一般从地址栏获取url。ajax是一个局部刷新的异步请求,我们不能从地址栏获取ajax的url,要借助抓包工具获取地址。

10. 综合项目实战

需求:爬取搜狗知乎某一个词条对应一定范围页码表示的页面

import requests
import os # 创建一个文件夹
if not os.path.exists('./pages'):
os.mkdir('./pages') word = input('enter a word:') # 动态指定页码的范围
start_page_number = int(input('enter a start page number'))
end_page_number = int(input('enter a end page nunber')) # 自定义请求头信息
headers = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
} # 1. 指定url:设计成一个具有通用的url
url = 'https://zhihu.sogou.com/zhihu'
for page in range(start_page_number,end_page_number + 1):
params = {
'query':word,
'page':page,
'ie':'utf-8',
}
response = requests.get(url=url,params=params,headers=headers) # 获取响应中的页面数据(指定页码(page))
page_text = response.text # 持久化存储
file_name = word + str(page) + '.html'
file_path = 'pages/' + file_name
with open(file_path,'w',encoding='utf-8') as fp:
fp.write(page_text)
print(f'第{page}页数据写入成功')

爬虫之requests模块基础的更多相关文章

  1. 03爬虫-requests模块基础(1)

    requests模块基础 什么是requests模块 requests模块是python中原生基于网络模拟浏览器发送请求模块.功能强大,用法简洁高效. 为什么要是用requests模块 用以前的url ...

  2. 孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块

    孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块 (完整学习过程屏幕记录视频地址在文末) 从今天起开始正式学习Python的爬虫. 今天已经初步了解了两个主要的模块: ...

  3. Python爬虫练习(requests模块)

    Python爬虫练习(requests模块) 关注公众号"轻松学编程"了解更多. 一.使用正则表达式解析页面和提取数据 1.爬取动态数据(js格式) 爬取http://fund.e ...

  4. 06.Python网络爬虫之requests模块(2)

    今日内容 session处理cookie proxies参数设置请求代理ip 基于线程池的数据爬取 知识点回顾 xpath的解析流程 bs4的解析流程 常用xpath表达式 常用bs4解析方法 引入 ...

  5. 网络爬虫之requests模块的使用+Github自动登入认证

    本篇博客将带领大家梳理爬虫中的requests模块,并结合Github的自动登入验证具体讲解requests模块的参数. 一.引入:   我们先来看如下的例子,初步体验下requests模块的使用: ...

  6. Python爬虫(requests模块)

     Requests是唯一的一个非转基因的Python HTTP库,人类可以安全享用. Requests基础学习 使用方法: 1.导入Requests模块: import requests 2.尝试用g ...

  7. 爬虫之requests模块

    requests模块 什么是requests模块 requests模块是python中原生的基于网络请求的模块,其主要作用是用来模拟浏览器发起请求.功能强大,用法简洁高效.在爬虫领域中占据着半壁江山的 ...

  8. 04.Python网络爬虫之requests模块(1)

    引入 Requests 唯一的一个非转基因的 Python HTTP 库,人类可以安全享用. 警告:非专业使用其他 HTTP 库会导致危险的副作用,包括:安全缺陷症.冗余代码症.重新发明轮子症.啃文档 ...

  9. Python 爬虫二 requests模块

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

随机推荐

  1. nagios centos7 rpm打包

    wget https://github.com/NagiosEnterprises/nagioscore/releases/download/nagios-4.3.1/nagios-4.3.1.tar ...

  2. June 14th 2017 Week 24th Wednesday

    Love looks not with the eyes, but with the mind. 爱,不在眼里,而在心中. Staring in her eyes and you will find ...

  3. MySQL学习(四)查询

    一.group_concat()函数.把groupby的分组中字段数据组合显示出来 select s_id , GROUP_CONCAT(要显示的字段名)  from table group by 分 ...

  4. mongodb文档替换

    对下面的文档做一个比较大的调整,将 friends.enemies两个字段移到 relationships子文档中. > db.people.insert({ "name" ...

  5. 设置IE浏览器的默认主页

    实现效果: 知识运用: RegistryKey类的GetValue方法 public Object GetValue (string name , Object defaultValue) name ...

  6. intelli j中如何重启tomcat,或者关掉tomcat?每次点run都提示jmx端口占用

    方法1.idea有时候会这样,我一般都是直接打开任务管理器,把java进程给杀掉就好了.

  7. 【转】Mac本地生成SSH Key 的方法

    1. 查看秘钥是否存在 打开终端查看是否已经存在SSH密钥:cd ~/.ssh 如果没有密钥则不会有此文件夹,有则备份删除,   也可以直接删除, 2.生成新的秘钥, 命令如下 $ssh-keygen ...

  8. node学习笔记(连载)

    这段时间玩了小程序.浏览器插件.koa建站,本来想写几篇文章总结一下的.迫于工作上有新需求要跟进,所以先写写读书笔记吧.公司九点上班,不过弹性工作时间,大家基本上九点半之前到.而我作为渣渣,八点半就到 ...

  9. 打开eclipse出现an error has occurred.see the loh file

    解决方案: 1,打开eclipse安装目录下的eclipse.ini文件:2,打开的文本文件最后添加一行--add-modules=ALL-SYSTEM3,保存重新打开Eclipse. 测试过已经ok

  10. 掘金上发现的有趣web api

    本篇文章主要选取了几个有趣且有用的webapi进行介绍,分别介绍其用法.用处以及浏览器支持度 page lifecycle onlineState(网络状态) device orientation(陀 ...