一 requests模块

  • 概念:

    • python中原生的基于网络请求的模块,模拟浏览器进行请求发送,获取页面数据
  • 安装: pip install requests

二 requests使用的步骤

  • 1 指定url
  • 2 基于requests模块请求发送
  • 3 获取响应对象中的数据值(text)
  • 4 持久化储存

三 反反爬

  • 1 设置ip
  • 2 设置UA
import requests

word = input('请你输入你要查的词')

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

params = {
'query': word
} heards = {
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
} response = requests.get(url=url, params=params,heards=heards,proxies={'https': '62.103.68.8:8080'}) ######UA 和 IP page_tail = response.text filename = word + '.html' with open(filename, 'w', encoding='utf-8') as f:
f.write(page_tail)

四  示例

No.1基于requests模块的get请求 

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

import requests

# 1 指定url
url = 'https://www.sogou.com/'
# 2 基于ruquests模块发送请求
response = requests.get(url=url)
# 3 获取响应对象的数据值
page_text = response.text
# 4 持久化存储
with open('./sogou.html','w',encoding='utf-8') as f:
f.write(page_text)

注意: 对于上面的代码

response.content             返回二进制的页面数据
response.headers 返回响应头信息
response.status_code 返回响应200
response.url 返回是地址
response.encoding 返回的是响应对象中存储数据的原始编码程序

需求2:爬取搜狗指定词搜索后的页面数据

import requests

word = input('请你输入你要查的词')
url = 'https://www.sogou.com/web' param = {
'query': word
}
response = requests.get(url=url, params=param) page_text = response.text
filename = word+'.html'
with open(filename, 'w', encoding='utf-8') as f:
f.write(page_text)

No.2基于requests模块的post请求  

需求3:登录豆瓣电影,爬取登录成功后的页面数据

# 依照我们上面所说的步骤
import requests url = 'https://www.douban.com/accounts/login' data = { # 在浏览器中找
"source": "index_nav",
"form_email": "xxxxxxxxx",
"form_password": "xxxxxxxxx"
} response = requests.post(url=url,data=data) page_text = response.text with open('douban.html', 'w', encoding='utf-8') as f:
f.write(page_text)

需求4:

基于requests模块ajax的get请求-------爬取豆瓣电影分类排行榜 https://movie.douban.com/中的电影详情数据 
import requests

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

param = {                               #携带的数据
'type': '',
'interval_id': '100:90',
'action': '',
'start': '',
'limit': '',
} response = requests.get(url=url, params=param})
print(response.text)

需求5:基于requests模块ajax的post请求-------------------------爬取肯德基餐厅查询http://www.kfc.com.cn/kfccda/index.aspx中指定地点的餐厅数据

import requests

url = ' http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword'
city = input('请输入你要查的城市')
data = {
'cname': '',
'pid': '',
'keyword': city,
'pageIndex': '',
'pageSize': '',
}
response = requests.post(url=url, data=data)
print(response.text)

需求6:简单的爬取博客园前几页

import requests
import os url = 'https://www.cnblogs.com/#p'
if not os.path.exists('boke'):
os.mkdir('boke') start_page = int(input('enter a start page:'))
end_page = int(input('enter a end page:')) for page in range(start_page, end_page + 1):
url = url + str(page)
response = requests.get(url=url, proxies={'https': '62.103.68.8:8080'})
page_text = response.text fileName = str(page) + '.html'
filePath = './boke/' + fileName
with open(filePath, 'w', encoding='utf-8') as f:
f.write(page_text)
print('第%s页打印' % page)
# 根据实际情况   本段代码所保存的html,是同一个(第一页的内容),
# 我们从页面抓包可以知道,它在第二页的时候发送了一个post请求

import requests
import os url = "http://www.cnblogs.com/mvc/AggSite/PostList.aspx" # url
if not os.path.exists('boke'):
os.mkdir('boke') start_page = int(input('enter a start page:'))
end_page = int(input('enter a end page:')) for page in range(start_page, end_page+1):
data = {
"CategoryType": "SiteHome",
"ParentCategoryId": 0,
"CategoryId": 808,
"PageIndex": page,
"TotalPostCount": 4000,
"ItemListActionName": "PostList"
} res = requests.post(url=url, data=data, verify=False)
page_text = res.text fileName = str(page) + '.html'
filePath = './boke/' + fileName
with open(filePath, 'w', encoding='gbk') as f:
f.write(page_text)
print('第%s页打印' % page)

  


												

(二)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. python网络爬虫之二requests模块

    requests http请求库 requests是基于python内置的urllib3来编写的,它比urllib更加方便,特别是在添加headers, post请求,以及cookies的设置上,处理 ...

  4. 爬虫——requests模块

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

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

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

  6. requests模块--python发送http请求

    requests模块 在Python内置模块(urllib.urllib2.httplib)的基础上进行了高度的封装,从而使得Pythoner更好的进行http请求,使用Requests可以轻而易举的 ...

  7. python爬虫之requests模块介绍

    介绍 #介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3) #注意:requests库发送请求将网页内容下 ...

  8. 爬虫 requests模块的其他用法 抽屉网线程池回调爬取+保存实例,gihub登陆实例

    requests模块的其他用法 #通常我们在发送请求时都需要带上请求头,请求头是将自身伪装成浏览器的关键,常见的有用的请求头如下 Host Referer #大型网站通常都会根据该参数判断请求的来源 ...

  9. 爬虫 requests 模块

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

随机推荐

  1. 五、java基础-关键字this_static_super_abstract_final,finalize()方法finally语句块

    1.关键字this 含义:this 是一个引用类型,代表当前对象,引用类型里面必然保存内存地址,在堆中的每个对象中存储,保存内存地址指向自身. 用法: 1)this可以用在成员方法中,里面保存内存地址 ...

  2. 移动端触摸touchstart监听事件

    click.mousedown等事件适用于PC端,在移动端会有一定时间的延迟,所以更好的优化移动端体验,要用touch事件, 1.首先要添加一个监听事件,监听移动端行为 element.addEven ...

  3. 在CentOS中配置java jdk环境

    方法一. 1.查看yum库中都有哪些jdk版本(暂时只发现了openjdk) [root@localhost ~]# yum search java|grep jdkldapjdk-javadoc.x ...

  4. go语言的基础类型

    1.布尔类型:bool 2.整型:int8,byte,int16,int,uint,uintptr等 3.浮点类型:float32.float64 4.复数类型:complex64,complex12 ...

  5. 前端学习笔记系列一:10整体移动vscode代码块、VSCode 使用 stylus,配置格式化设置、在vue项目中引入bootstrap

    1.整体移动vscode代码块 凭借操作的经验我们能够轻松地知道将代码整体往右移只需选中代码按Tab键即可.其实往左移也很简单: 选中之后按下 shift+Tab键 即可. 2.VSCode 使用 s ...

  6. 前端学习笔记系列一:7 在vscode中根据vue等模板生成代码

    目标:希望每次新建.vue文件后,VSCODE能够根据配置,自动生成我们想要的内容. 方法:打开VSCODE编辑器,依次选择“文件 -> 首选项 -> 用户代码片段”,此时,会弹出一个搜索 ...

  7. dateDiff 用法

    SELECT DATEDIFF(yy,'2018-3-10 16:40:00',getdate())SELECT DATEDIFF(mm,'2018-3-10 16:40:00',getdate()) ...

  8. NO16 第二关课后考试-aw-F-过滤已知的一级目录

    ·总结的经验:1.学会总结时学好运维的重要前提.2.通过案列或例子来总结一个技术点或者命令.3.画一张逻辑图,形象的卡通记忆这个知识技术点.4.通过管道过滤数据时,最好先输出结果,然后回退再使用管道看 ...

  9. sql 经纬度范围检索(谷歌方案)

    SELECT id, ( * acos ( //公里: 6371 英里: 3959 cos ( radians(78.3232) ) * cos( radians( 数据库纬度字段) ) * cos( ...

  10. spring#事件发布订阅

    1. 如果在应用中发生了某些事件,事件会被拦截和处理就好了,这样就有了很大的灵活性,至少代码不会紧密的耦合在一起, 代码的解耦就是业务的解耦,业务A的代码不用手动的调用业务B的代码,业务B只需要监听相 ...