当前位置:网站首页>Scrapy框架进阶学习
Scrapy框架进阶学习
2022-04-22 16:34:00 【yiniuyun】
之前有跟大家分享过爬虫中关于Scrapy框架的使用,今天我们再一次进阶的讲述下这个框架,在爬虫众多框架里面Scrapy 属于佼佼者。它为我们提前想好了很多步骤和要处理的边边角角的问题,而使用者可以专心于处理解析页面、分析请求这种最核心的事情。
Scrapy 是一个纯 Python 实现的、流行的网络爬虫框架,它使用了一些高级功能来简化网页的抓取,能让我们的爬虫更加的规范、高效。今天我们继续以实战来学习爬虫,这次我们试着用Scrapy爬取的网站是小红书:https://www.xiaohongshu.com/。这个网站不用多说大家都知道它的反爬机制很严,所以就算只是实践学习,我们也要做好反爬措施,基本的像随机ua,cookie,代理IP都需要做好。像小红书这样的网站代理IP的使要求是很高的,所以需要找高质量的代理。今天我们就继续使用它来获取小红书数据。完整的爬虫代码如下:
#! -*- encoding:utf-8 -*-
import base64
import sys
import random
PY3 = sys.version_info[0] >= 3
def base64ify(bytes_or_str):
if PY3 and isinstance(bytes_or_str, str):
input_bytes = bytes_or_str.encode('utf8')
else:
input_bytes = bytes_or_str
output_bytes = base64.urlsafe_b64encode(input_bytes)
if PY3:
return output_bytes.decode('ascii')
else:
return output_bytes
class ProxyMiddleware(object):
def process_request(self, request, spider):
# 代理服务器(产品官网 www.16yun.cn)
proxyHost = "t.16yun.cn"
proxyPort = "31111"
# 代理验证信息
proxyUser = "username"
proxyPass = "password"
request.meta['proxy'] = "http://{0}:{1}".format(proxyHost,proxyPort)
# 添加验证头
encoded_user_pass = base64ify(proxyUser + ":" + proxyPass)
request.headers['Proxy-Authorization'] = 'Basic ' + encoded_user_pass
# 设置IP切换头(根据需求)
tunnel = random.randint(1,10000)
request.headers['Proxy-Tunnel'] = str(tunnel)
修改项目配置文件 (./项目名/settings.py)
DOWNLOADER_MIDDLEWARES = {
'项目名.middlewares.ProxyMiddleware': 100,
}
这里重点讲下 Scrapy除了解析想要的数据,其他的一切都是 Scrapy 替你完成的。这就是 Scrapy 的最大优势,requests 去哪了?不需要,只要把链接交给 Scrapy 就会自动帮你完成请求。
版权声明
本文为[yiniuyun]所创,转载请带上原文链接,感谢
https://cloud.tencent.com/developer/article/1985839
边栏推荐
- Web测试需要注意什么?
- 如何登录MySQL
- Blue Bridge Cup practice 013
- Merge two ordered linked lists (delete the same content)
- 从思维走向实践,数字化转型 IT 经营的成功路径
- Huawei machine test questions -- hj54 expression evaluation
- 渗透测试&网络&CTF面试题整理
- 实验四、数据挖掘之KNN,Naive Bayes
- DB107-ASEMI整流桥详细数据
- TM of NLP: Based on gensim library, call 20newsgr to learn doc topic distribution and save it as train SVM LDA txt、test-svm-lda. txt
猜你喜欢

MiniUSB管脚接口引脚定义

ICMP and IPv6 global unicast address dynamic allocation

蓝桥杯练习017
![[arm assembly] how to judge the input data?](/img/bb/7273d2edf33cfa27246df28c925520.png)
[arm assembly] how to judge the input data?

NFT platform security guide

Blue Bridge Cup practice 017

C ODBC loads the files of one folder into the blob column of Oracle database and downloads the blob column to another folder

RTP packaging and unpacking of webrtc

MySQL日志

NLP之TM:基于gensim库调用20newsgr学习doc-topic分布并保存为train-svm-lda.txt、test-svm-lda.txt
随机推荐
查询表中distinct 数据 数量 count mysql oracle 取指定条数
Detailed data of db107-asemi rectifier bridge
SolidWorks insert socket head cap screw
如何登录MySQL
Blue Bridge Cup practice 019
实验三 FFT及其在卷积计算和谱分析中的应用
The number of distinct data in the query table count MySQL Oracle takes the specified number
步进伺服电机驱动的系统用于旋转角度控制分子分母计算方法
持续交付-Jenkinsfile 语法
C determines whether the database return record is a null DbNull class DbNull Value. Equals(row[fieldName]
动态拖动两个div的宽度
漫谈同源策略(SOP)和跨域资源共享(CORS)
Huawei machine test questions -- hj54 expression evaluation
Blue Bridge Cup practice 014
Gdoi2022 retirement (from heaven to earth)
蓝桥杯练习018
i.MX6ULL驱动开发 | 21 - 按键驱动使用 input 子系统上报事件
华为机试题——HJ60 查找组成一个偶数最接近的两个素数
Where is the dimension association table of Flink SQL? 800W dimension table
接口测试框架实战 | 流程封装与基于加密接口的测试用例设计