安装scrapy:
| 1.pip3 install wheel |
| 2.下载twisted 网址:https://www.lfd.uci.edu/~gohlke/pythonlibs/ |
| 3.cmd进入你所下载文件的目录,pip3 install 你下的文件名 |
| 4.pip3 install pywin32 |
| 5.pip3 install scrapy |
| |
| 大功告成!!! |

创建scrapy项目
| |
| |
| |
| |
| import scrapy |
| class FirstSpider(scrapy.Spider): |
| |
| name = 'first' |
| |
| |
| |
| start_urls = ['https://www.baidu.com/'] |
| |
| def parse(self, response): |
| print(response) |
| |
第一次运行scrapy问题
配置UA
| 去所创建项目的settings.py配置 |
| USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36' |
爬虫文件的返回值(response)
| |
| div_list = response.xpath('') |
| |
| |
| 提取Selector的文本 |
| 1.xpath('//xx/x/text()').extract() |
| 2.xpath('//xx/x/text()').extract_first() |
持久化存储
- 步骤:

| 一.基于终端命令进行存储 |
| |
| |
| '''在终端运行文件 :scrapy crawl 爬虫文件名 -o(存储文件命令) xx.txt(不能存储为txt形式,可以先运行此命令,系统会提示可以将数据存储为何种形式)''' 如上图所示 |
| |
| 二.基于数据库存储 |
| item.py: |
| class BossproItem(scrapy.Item): |
| '''define the fields for your item here like: |
| name = scrapy.Field() |
| title = scrapy.Field() |
| salary = scrapy.Field() |
| company = scrapy.Field() |
| ''' |
| |
| 爬虫文件:实例化一个BossproItem对象 |
| |
| |
| .... |
| |
| yield item |
| |
| pipelines.py: |
| 如下图⬇⬇⬇⬇ |
| 重写父类的方法:open_spider(self,spider)与close_spider(self,spider) |
| |
| |
基于文件存储
基于mysql存储

基于Redis存储


scrapy的cookie处理
| 我们知道,一些网站的进入需要cookie |
| 例如:QQ空间的登录等...如果我们爬虫需要模拟登录后需要拿到详情页面,需要携带cookie, 那么scrapy是如何处理cookie? |
| 在一般情况下,我们会在登录后通过请求头拿到cookie,然后将cookie加入进headers中,然后拿到用户详情页面数据 |
| scrapy不需要,只需要将配置修改 初始是注释(自动处理cookie),如果你发送请求时scrapy会携带cookie,不需要我们主动去获取cookie |
| 是不是很方便啊 |

scrapy发送post请求

scrapy请求传参

提升爬取的效率
通常,我们使用多线程可以提升效率
scrapy可以通过修改配置提升爬取的效率
| 修改配置: |
| |
| |
| CONCURRENT_REQUESTS = 10 |
| |
| |
| |
| LOG_LEVEL = ‘INFO’ LOG_LEVEL = 'ERROR' |
| |
| |
| |
| COOKIES_ENABLED = False |
| |
| |
| |
| RETRY_ENABLED = False |
| |
| |
| |
| DOWNLOAD_TIMEOUT = 10 超时时间为10s |
| |
UA池与代理池
先了解scrapy的框架
https://www.jianshu.com/p/8e78dfa7c368

看代码吧(说不太清,或者看书)
CrawlSpider
| 'crawlspider是Spider的派生类(一个子类),Spider类的设计原则是只爬取start_url列表中的网页,而CrawlSpider类定义了一些规则(rule)来提供跟进link的方便的机制,从爬取的网页中获取link并继续爬取的工作更适合。' |
crawlspider的使用
https://www.jianshu.com/p/a6a08b4f7c04
| 一.创建项目: scrapy startsproject 项目名 |
| cd 项目名 |
| 创建爬虫文件:scrapy genspider -t crawl 文件名 起始url(www.xxx.com) |
例 :如下为创建的爬虫文件
| |
| import scrapy |
| |
| from scrapy.linkextractors import LinkExtractor |
| from scrapy.spiders import CrawlSpider, Rule |
| |
| |
| class CrawldemoSpider(CrawlSpider): |
| name = 'crawlDemo' |
| |
| start_urls = ['http://www.qiushibaike.com/'] |
| |
| |
| link = LinkExtractor(allow=r'/8hr/page/\d+') |
| |
| rules = ( |
| |
| Rule(link, callback='parse_item', follow=True), |
| ) |
| |
| def parse_item(self, response): |
| |
| divs = response.xpath('//div[@id="content-left"]/div') |
| for div in divs: |
| author = div.xpath('./div[@class="author clearfix"]/a[2]/h2/text()').extract_first() |
| print(author) |
| |
| <!-- CrawlSpider类和Spider类的最大不同是CrawlSpider多了一个rules属性,其作用是定义”提取动作“。在rules中可以包含一个或多个Rule对象,在Rule对象中包含了LinkExtractor对象。 --> |
分布式爬虫
-- 为了加快数据的爬取效率
| 步骤一. |
| 1.安装模块:pip3 install redis_scrapy |
| 2.创建项目 scrapy startsproject 项目名 |
| cd 项目名 |
| 3.创建爬虫文件:scrapy genspider 文件名 起始url(www.xxx.com) |
| 步骤二.修改爬虫文件 |
| 1.导入scrapy_redis模块: from scrapy_redis.spiders import RedisSpider |
| 2.将当前爬虫的父类修改成 RedisSpider |
| 3.将allowed_domains和start_urls注释(分布式,不能每一个爬虫文件都有一个起始url) |
| 4.添加一个新的属性 redis_key = "xxx",该属性表示的是可以被共享的调度器队列的名称 |
| 步骤三.修改配置文件(settings.py) |
| 1.保证爬虫文件所提交的请求都会被提交到共享的调度器的队列中 |
| SCHEDULER = "scrapy_redis.scheduler.Scheduler" |
| 2.保证爬虫文件提交的item会被提交到共享的管道中 |
| ITEM_PIPELINES = { |
| 'scrapy_redis.pipelines.RedisPipeline': 400 |
| } |
| 3.配置最终数据存储的redis数据库 |
| REDIS_HOST = 'redis服务的ip地址' |
| REDIS_PORT = 6379 |
| REDIS_ENCODING = ‘utf-8’ |
| REDIS_PARAMS = {‘password’:’123456’} |
| 4.redis数据库的配置文件进行配置:关闭保护模式和注释掉bind 127.0.0.1 |
| 5.开启redis服务和客户端 |
| 6.执行爬虫文件:scrapy runspider xxx.py |
| 7.向调度器队列中仍入一个起始的url: |
| |
| |
| |
| |
| settings中加入如下配置: |
| |
| |
| DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter" |
| |
| SCHEDULER = "scrapy_redis.scheduler.Scheduler" |
| |
| SCHEDULER_PERSIST = True |
| |
| 运行文件: |
| ***切换到spider目录下,运行命令: |
| scrapy runspider 爬虫文件.py |
| 运行文件后会卡在listen处 ---> 等待一个url |
| 执行步骤三中的第7步,程序将继续向下执行 |
增量式爬虫
| 增量式爬虫:每次爬取网站中更新的数据. |
| 定时爬虫:使用脚本在指定时间内进行一次数据(最新更新)的爬取. |
| |
| 爬取数据的流程: |
| 1.指定url |
| 2.根据指定的url发请求,获取页面数据 |
| 3.持久化存储 |
| |
| 案例: |
| 实现增量式爬虫的方案: |
| 1.在发送请求之前,判断url之前是否爬取过 |
| a.将即将进行爬取的数据对应的url存储到redis的set中. |
| |
| 2.根据爬取到的数据进行重复过滤,然后在进行持久化存储 |
| b.将爬取到的数据给其生成一个唯一的标识(可以将该标识作为mysql的列.可以将该标识存储到redis的set中) |
自定义增量式爬虫
| 将爬取的url或数据进行唯一标识 |
| ex = redis.sadd(key,value) |
| 若ex返回值为1,则该数据将会放入redis数据库中 |
| 若返回为0,则说明redis数据库中存在该数据 |
| 判断ex的返回值可以进行爬取网页更新的数据 |
| |
| |
| |
定时爬虫
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)