当前位置:网站首页>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])
边栏推荐
- PyG builds R-GCN to realize node classification
- 我的Go+语言初体验——祝福留言小系统,让她也可以感受到你的祝福
- MySQL data query (subtotal and sorting)
- Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (6) Question Opening and Defense PPT
- Pytorch framework learning record 7 - convolutional layer
- Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Product (8) Graduation Design Thesis Template
- 2021山东省网络搭建与应用赛项试题
- cv2.polylines
- Atomic Guarantees of Redis Distributed Locks
- phpoffice edit excel document
猜你喜欢

Pytorch框架学习记录2——TensorBoard的使用

验证addShutdownHook钩子生效

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

Based on all volunteers - H and D1 XR806 rare plant monitoring device

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

ospf 综合实验(重发布,特殊区域)

Atomic Guarantees of Redis Distributed Locks

Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (4) Opening Report

机器学习:知道通过低方差过滤实现降维过程

国内首家沉浸式高逼真元宇宙,希元宇宙正式上线
随机推荐
MYSQL 唯一约束
What are Redis server startup after the operation?
RRU, BBU, AAU
【C进阶】数组传参与函数指针
(6) "Digital Electricity" - Diodes and CMOS Gate Circuits (Introduction)
Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (3) Background Functions
The difference between forward and redirect
handler+message【消息机制】
RRU、BBU、AAU
sublime text 3 settings
【翻译】Envoy Fundamentals,这是一个培训课程,使人们能够更快地采用Envoy Proxy。...
OA Project Pending Meeting & History Meeting & All Meetings
mysql 结构、索引详解
图像视角矫正之透视变换矩阵(单应矩阵)/findHomography 与 getPerspectiveTransformd的区别
PyG builds R-GCN to realize node classification
Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (6) Question Opening Reply PPT
Azure 开发者新闻快讯丨开发者7月大事记一览
Pytorch framework to study record 6 - the torch. Nn. The Module and the torch nn. Functional. The use of conv2d
Uptime Monitoring: How to Ensure Network Device Uptime
Anti-shake and throttling