当前位置:网站首页>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])
边栏推荐
- spicy (two) unit hooks
- LeetCode 114. Expand Binary Tree into Linked List (One Question Three Eats)
- SQL Server data type conversion function cast () and convert () explanation
- 数据库概论 - MySQL的简单介绍
- 【驱动】udev设置GPIO加载后所有者、所属组和权限
- QT(39)-vs开发qt程序提示无法打开源文件
- Redis【超详解!!!】
- Pytorch框架学习记录5——DataLoader的使用
- 恐造成下一个“千年虫”的闰秒,遭科技巨头们联合抵制
- How to Effectively Conduct Retrospective Meetings (Part 1)?
猜你喜欢

The leap second that may cause the next "Millennium Bug" is boycotted by tech giants

【Untitled】
![[The Mystery of Cloud Native] Cloud Native Background && Definition && Detailed explanation of related technologies?](/img/eb/0cd6891fcc00d2c01ba8bd7f8d0822.png)
[The Mystery of Cloud Native] Cloud Native Background && Definition && Detailed explanation of related technologies?

Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Product (2) Mini Program Function

High Concurrency Framework Disruptor

How to extract year, month and day data in date type in SQL Server

How does the AI intelligent security video platform EasyCVR configure the simultaneous transmission of audio and video?

Has been empty, a straightforward, continue to copy the top off!

代码开源设计实现思路

Many overseas authoritative media hotly discuss TRON: laying the foundation for the decentralization of the Internet
随机推荐
MySQL 字符串拼接 - 多种字符串拼接实战案例
sql语句-如何以一个表中的数据为条件据查询另一个表中的数据
[Driver] udev sets the owner, group and permissions after GPIO is loaded
MYSQL 唯一约束
Pytorch framework learning record 4 - the use of datasets (torchvision.dataset)
使用EFR32作为Zigbee/Thread的sniffer的用法
MySQL data query (subtotal and sorting)
宇宙的尽头是银行?聊聊在银行做软件测试的那些事
High Concurrency Framework Disruptor
state space representation
基于OpenCV实现的图像拼接(配准)案例
弘玑再度入围Gartner 2022 RPA魔力象限并实现位置大幅跃升
【转】Swift 中的面向协议编程:引言
Thymeleaf简介
C. Travelling Salesman and Special Numbers (二进制 + 组合数)
(redistribute, special comprehensive experiment ospf area)
mysql structure, index details
Why is the Kirin 9000 5G version suddenly back in stock?
Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (7) Interim Inspection Report
【Untitled】