博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Scrapy-redis分布式爬虫爬取豆瓣电影详情页
阅读量:7049 次
发布时间:2019-06-28

本文共 3256 字,大约阅读时间需要 10 分钟。

平时爬虫一般都使用Scrapy框架,通常都是在一台机器上跑,爬取速度也不能达到预期效果,数据量小,而且很容易就会被封禁IP或者账号,这时候可以使用代理IP或者登录方式爬,然而代理IP很多时候都很鸡肋,除非使用付费版IP,但是和真实IP差别很大。这时候便有了Scrapy-redis分布式爬虫框架,它基于Scrapy改造,把Scrapy的调度器(scheduler)换成了Scrapy-redis的调度器,可以轻松达到目的,利用多台服务器来爬取数据,而且还可以自动去重,效率高。爬取的数据默认保存在redis缓存中,速度很快。

Scrapy工作原理:

1579698-20190524151625747-13291806.png

Scrapy-redis工作原理:

1579698-20190524151631360-286383183.png

中间的就是调度器

豆瓣电影简易分布式爬虫

我这里直接使用start_urls的方式,数据存入到Mysql中

class DoubanSpider(RedisSpider):    name = 'douban'    redis_key = 'douban:start_urls'    allowed_domains = ['douban.com']    def start_requests(self):        urls = get_urls()        for url in urls:            yield scrapy.Request(url=url, callback=self.parse)    def parse(self, response):        # item_loader = MovieItemLoader(item=MovieItem, response=response)        #        # item_loader.add_xpath('title', '')        item = MovieItem()        print(response.url)        item['movieId'] = int(response.url.split('subject/')[1].replace('/', ''))        item['title'] = response.xpath('//h1/span/text()').extract()[0]        item['year'] = response.xpath('//h1/span/text()').extract()[1].split('(')[1].split(')')[0] or '2019'        item['url'] = response.url        item['cover'] = response.xpath('//a[@class="nbgnbg"]/img/@src').extract()[0]        try:            item['director'] = response.xpath('//a[@rel="v:directedBy"]/text()').extract()[0] or '无'        except Exception:            item['director'] = '暂无'        item['major'] = '/'.join(response.xpath('//a[@rel="v:starring"]/text()').extract())        item['category'] = ','.join(response.xpath('//span[@property="v:genre"]/text()').extract())        item['time'] = ','.join(response.xpath('//span[@property="v:initialReleaseDate"]/text()').extract())        try:            item['duration'] = response.xpath('//span[@property="v:runtime"]/text()').extract()[0]        except Exception:            item['duration'] = '暂无'        item['score'] = response.xpath('//strong[@property="v:average"]/text()').extract()[0]        item['comment_nums'] = response.xpath('//span[@property="v:votes"]/text()').extract()[0] or 0        item['desc'] = response.xpath('//span[@property="v:summary"]/text()').extract()[0].strip()        actor_list = response.xpath('//ul[@class="celebrities-list from-subject __oneline"]/li/a/@title').extract()        actor_img_list = response.xpath('//ul[@class="celebrities-list from-subject __oneline"]/li/a/div/@style').extract()        actor_img_list = [i.split('url(')[1].replace(')', '') for i in actor_img_list]        item['actor_name_list'] = '----'.join(actor_list)        item['actor_img_list'] = '----'.join(actor_img_list)        yield item

settings.py文件

BOT_NAME = 'MovieSpider'SPIDER_MODULES = ['MovieSpider.spiders']NEWSPIDER_MODULE = 'MovieSpider.spiders'# REDIS_HOST = '127.0.0.1'# REDIS_PORT = 6379REDIS_URL = 'redis://username:password@xxx.xxx.xxx.xxx:6379'# Obey robots.txt rulesROBOTSTXT_OBEY = FalseSCHEDULER = "scrapy_redis.scheduler.Scheduler"# Ensure all spiders share same duplicates filter through redis.DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter"ITEM_PIPELINES = {    'scrapy_redis.pipelines.RedisPipeline': 300,    'MovieSpider.pipelines.MysqlPipeline': 200,}

这里只是为了多台服务器一起爬取,没有手动在redis中推入起始的URL

此时将爬虫项目上传到其他服务器上,一起开始

效果如下:

1579698-20190524151639443-73110896.png

转载于:https://www.cnblogs.com/PyKK2019/p/10918308.html

你可能感兴趣的文章
最长回文子串问题
查看>>
ssh客户端及基于key登陆
查看>>
echo命令
查看>>
图形语言 Kgo
查看>>
兄弟连第10节课
查看>>
调整Virtual Box硬盘大小
查看>>
case 格式
查看>>
Windows下Apache服务器中自动配置二级子域名
查看>>
【Tomcat】日常遇到的Tomcat报错及解决方法
查看>>
Transform Map - Ignore Row if any fields are empty
查看>>
SVG绘制loading效果
查看>>
在kubernets中搭建jenkins服务
查看>>
iEclipse-不只是Eclipse的开发者社区
查看>>
Oracle个人的一些记录
查看>>
20.分屏查看命令 less命令
查看>>
感谢付费客户不覺流年似水(271558528) 对C#ASP.NET通用权限管理组件的改进意见,已修正...
查看>>
MySQL5.6.17学习笔记(四)复合分区及分区管理
查看>>
android 让 TextView 自带滚动条
查看>>
PHP过滤常见html标签的正则表达式
查看>>
注册与登录界面的美化
查看>>