当前位置:网站首页>1. 获取数据-requests.get()
1. 获取数据-requests.get()
2022-07-30 04:15:00 【无薪法师】
1、爬虫的工作原理
获取数据–解析数据–提取数据–存储数据

2、获取数据
本质就是通过URL去向服务器发出请求,服务器再把相关内容封装成一个Response对象返回给我们,这是通过requests.get()实现的。而我们获取到的Response对象下有四个常用的方法(status_code、content、text、encoding)。
3、requests.get()
import requests #引入requests模块
res = requests.get(‘url’) # 向服务器请求数据,服务器返回的结果是个Response对象
print(type(res)) # 终端显示:<class ‘requests.models.Response’>
这代表着res是一个对象,属于requests.models.Response类。
3、response.status_code
用法:print(变量.status_code),
用来检查请求是否正确响应,如果响应状态码为200,即代表请求成功。

响应状态码表示服务器对请求的响应结果。例如,200代表服务器响应成功,403代表禁止访问,404代表页面未找到,408代表请求超时。浏览器会根据状态码做出相应的处理。在爬虫中,可以根据状态码来判断服务器的状态,如果状态码为200,则继续处理数据,否则直接忽略。
4、response.content
response.content能把Response对象的内容以二进制数据的形式返回,适用于图片、音频、视频的下载,示例:
import requests
#图片地址
URL=‘‘https://img1.baidu.com/it/u=2076064484,1314795796&fm=253&fmt=auto&app=120&f=JPEGw=531&h=309’’
发出请求,并把返回的结果放在变量res中
res = requests.get(url)
# 把Reponse对象的内容以二进制数据的形式返回
pic = res.content
# 下载一个图片文件并命名为picture.jpg, 图片内容需要以二进制wb只写。
with open(r’C:UsersAveryDesktop estpicture.jpg’, ‘wb’) as f:
获取pic的二进制内容,写入f
f.write(pic)
这样我们图片就下载成功了!
5、response.text
response.text这个方法可以把Response对象的内容以字符串的形式返回,适用于文字、网页源代码的下载。示例如下:
import requests
文章地址
url = ‘https://localprod.pandateacher.com/python-manuscript/crawler-html/sanguo.md’
发出请求,并把返回的结果放在变量res中
res = requests.get(url)
# 把Response对象的内容以字符串的形式返回
novel = res.text
#打印变量
print(novel[0:170])
6、response.encoding
response.encoding方法,它能帮我们定义Response对象的编码,示例如下:
import requests
文章地址
url = ‘https://localprod.pandateacher.com/python-manuscript/crawler-html/sanguo.md’
发出请求,并把返回的结果放在变量res中
res = requests.get(url)
# 定义response对应的编码为utf-8
res.encoding = ‘utf-8’
把Response对象的内容以字符串的形式返回
novel = res.text
打印变量
print(novel[0:170])
边栏推荐
- Pytorch框架学习记录4——数据集的使用(torchvision.dataset)
- phpoffice edit excel document
- Reverse Theory Knowledge 3 [UI Modification]
- OA Project Pending Meeting & History Meeting & All Meetings
- 2022-07-29 Group 4 Self-cultivation class study notes (every day)
- The difference between forward and redirect
- cv2.polylines
- golang中如何比较struct,slice,map是否相等以及几种对比方法的区别
- The underlying mechanism of the function
- Data Lake: Data Integration Tool DataX
猜你喜欢

ospf 导图

厦门感芯科技MC3172(1):介绍和环境搭建

Pytorch framework learning record 7 - convolutional layer

Pytorch框架学习记录7——卷积层

MySQL 安装报错的解决方法

恐造成下一个“千年虫”的闰秒,遭科技巨头们联合抵制

(redistribute, special comprehensive experiment ospf area)

When the EasyNVR platform is cascaded to the EasyCVR, why can't the video be played after a while?

Reverse Analysis Practice 2

Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (3) Background Functions
随机推荐
厦门感芯科技MC3172(1):介绍和环境搭建
2022.7.29-----leetcode.593
Flutter record learning different animation (2)
Pytorch框架学习记录4——数据集的使用(torchvision.dataset)
MySql 怎么查出符合条件的最新的数据行?
Eureka注册中心
Anti-shake and throttling
C. Travelling Salesman and Special Numbers (二进制 + 组合数)
day10--install mysql on linux
我的Go+语言初体验——祝福留言小系统,让她也可以感受到你的祝福
The underlying mechanism of the function
使用EFR32作为Zigbee/Thread的sniffer的用法
Thinkphp 5.0.24变量覆盖漏洞导致RCE分析
The first immersive and high-fidelity metaverse in China, Xiyuan Universe is officially launched
权值线段树+线段树分裂/合并+CF1659D
为什么突然间麒麟 9000 5G 版本,又有库存了?
WEB 渗透之信息收集
vscode debugging and remote
代码开源设计实现思路
逆向理论知识3【UI修改篇】