Python进行Scrapy-redis分布式爬取的方法?这个问题可能是我们日常学习或工作经常见到的。希望通过这个问题能让你收获颇深。下面是小编给大家带来的参考内容,让我们一起来看看吧!
平时爬虫一般都使用Scrapy框架,通常都是在一台机器上跑,爬取速度也不能达到预期效果,数据量小,而且很容易就会被封禁IP或者账号,这时候可以使用代理IP或者登录方式爬,然而代理IP很多时候都很鸡肋,除非使用付费版IP,但是和真实IP差别很大。这时候便有了Scrapy-redis分布式爬虫框架,它基于Scrapy改造,把Scrapy的调度器(scheduler)换成了Scrapy-redis的调度器,可以轻松达到目的,利用多台服务器来爬取数据,而且还可以自动去重,效率高。爬取的数据默认保存在redis缓存中,速度很快。
Scrapy工作原理:
Scrapy-redis工作原理:
中间的就是调度器
豆瓣电影简易分布式爬虫
我这里直接使用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('//h2/span/text()').extract()[0]
item['year'] = response.xpath('//h2/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 = 6379
REDIS_URL = 'redis://username:password@xxx.xxx.xxx.xxx:6379'
# Obey robots.txt rules
ROBOTSTXT_OBEY = False
SCHEDULER = "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
此时将爬虫项目上传到其他服务器上,一起开始
效果如下:
感谢各位的阅读!看完上述内容,你们对Python进行Scrapy-redis分布式爬取的方法大概了解了吗?希望文章内容对大家有所帮助。如果想了解更多相关文章内容,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。