Scrapy10行代码爬取 2W+ 电影天堂电影
前言
scrapy,俗称「西瓜皮」,它 的强大相信大家已早有耳闻,之前用的时候只是感觉它爬取效率高,内部的异步封装很友好。殊不知我看到的知识冰山一角,它还有很多提高开发效率的功能,今天就以一个实例带大家窥探一二。
工具环境
阅读此文可能需要对scrapy框架有基本的了解,对xpath解析有一个基本的了解
爬取思路
爬取站点:dytt8.net/
这样的站点我一眼望过去就想给站长打Q币,为什么啊?这站点简直是为爬虫而生啊?你看,一点花里胡哨的东西都没有,我们小眼一瞅便知,和我们不相关的就只是游戏部分的资源。我们需要如果要进入爬取更多的页面就只需要点击导航页
当我点开这些导航页,我都要惊喜的哭了,你们谁也别拦我,我要给站长打钱,对爬虫简直太友好了,我们其实只需要跟踪带有index页面的链接,然后深入下去爬取内页即可
当我们到达内页,再看下哪里可以深入,这里其实也很简单,翻到最底下发现,只需要继续跟踪下一页链接即可
新建项目文件
scrapy startproject www_dytt8_net
得到一个如下结构的文件目录 ????
创建crawl模板样式的爬虫demo
先命令行进入到spiders目录下
1cd www_dytt8_net/www_dytt8_net/spiders
然后创建爬虫模板
1scrapy genspider -t crawl dytt8 www.dytt8.net
得到如下文件 ????
配置跟踪页面规则
爬取思路中,我们已经介绍过了,只需要跟踪首页链接中,带有index的页面,进入到内页后,我们继续跟踪下一页即可遍历所有电影页面。简单了,我们配置一下rule,让爬爬开启追踪模式,黑喂狗→
1Rule(LinkExtractor(deny=r'.*game.*', allow='.*/index\.html'))r'.*game.*', allow='.*/index\.html'))
这里的LinkExtractor
的作用可以提取一些链接,通过对象提供的参数,方便我们进行追踪和过滤不需要的链接。如这里的deny表示我们不需要其中包含game字段的链接,而allow表示我们允许index的链接
其实内部还支持很多别的提取规则,小伙伴们可以去官方文档看看,全是爬虫的大杀器:
https://scrapy.readthedocs.io/en/latest/topics/link-extractors.html#module-scrapy.linkextractors.lxmlhtml
导航页点击进去的下一页
1Rule(LinkExtractor(restrict_xpaths=u'//a[text()="下一页"]'))u'//a[text()="下一页"]'))
简直良心啊有木有?这要是平时,我们得写好几个代码段呐。这里就真的只需要一行,restrict_xpaths
支持xpath语法,提取标签页为下一页的所有a标签内的链接,这里其实我过滤掉了callback参数,表示这个页面跟踪后,交给谁处理下一步,还有一个参数是follow
可以设为True
或 False
表示接下来要不要对这个页面进行进一步的追踪
提取文章页链接,交由解析函数处理
1Rule(LinkExtractor(allow=r'.*/\d+/\d+\.html', deny=r".*game.*"), callback='parse_item', follow=True)r'.*/\d+/\d+\.html', deny=r".*game.*"), callback='parse_item', follow=True)
我们将页面中有这种形式的页面提取出来{数字}/{数字}.html
的页面提取出来其实就是详情文章页面,注意这里需要过滤掉是游戏的页面,所有增加拒绝跟踪链接的规则deny=r".*game.*"
,最后将提取到的链接回调给parse_item
处理
提取所需关键信息
在items.py内定义我们需要提取的字段
1import scrapyclass WwwDytt8NetItem(scrapy.Item):2 # define the fields for your item here like:3 # name = scrapy.Field()4 title = scrapy.Field()5 publish_time = scrapy.Field()6 images = scrapy.Field()7 download_links = scrapy.Field()8 contents = scrapy.Field()import scrapyclass WwwDytt8NetItem(scrapy.Item):
2 # define the fields for your item here like:
3 # name = scrapy.Field()
4 title = scrapy.Field()
5 publish_time = scrapy.Field()
6 images = scrapy.Field()
7 download_links = scrapy.Field()
8 contents = scrapy.Field()
接着将spiders/dytt8.py中的回调函数中的提取规则写出来
1def parse_item(self, response):2 item = WwwDytt8NetItem()3 item['title'] = response.xpath('//div[@class="title_all"]/h1/font/text()').extract_first()4 item['publish_time'] = response.xpath('//div[@class="co_content8"]/ul/text()').extract_first().strip().replace('发布时间:', '')5 imgs_xpath = response.xpath('//div[@id="Zoom"]//img')6 item['images'] = [i.xpath('./@src').extract_first() for i in imgs_xpath if i.xpath('./@src')]7 item['download_links'] = re.compile('<a href="(ftp://.*?)">').findall(response.text)8 item['contents'] = [i.strip().replace('\n', '').replace('\r', '') for i in response.xpath('string(//div[@id="Zoom"])').extract()]9 yield item def parse_item(self, response):
2 item = WwwDytt8NetItem()
3 item['title'] = response.xpath('//div[@class="title_all"]/h1/font/text()').extract_first()
4 item['publish_time'] = response.xpath('//div[@class="co_content8"]/ul/text()').extract_first().strip().replace('发布时间:', '')
5 imgs_xpath = response.xpath('//div[@id="Zoom"]//img')
6 item['images'] = [i.xpath('./@src').extract_first() for i in imgs_xpath if i.xpath('./@src')]
7 item['download_links'] = re.compile('<a href="(ftp://.*?)">').findall(response.text)
8 item['contents'] = [i.strip().replace('\n', '').replace('\r', '') for i in response.xpath('string(//div[@id="Zoom"])').extract()]
9 yield item
启动脚本看下运行状态
1scrapy crawl dytt8scrapy crawl dytt8
感受爬爬的力量吧????
数据库存储
这里介绍一下scrapy-mongodb,也是超级好用,省去了自己链接和配置的麻烦,没有下载的童鞋,可以pip安装下
我们只需要settings.py中,配置如下中间键
1ITEM_PIPELINES = {2 # 'www_dytt8_net.pipelines.WwwDytt8NetPipeline': 300,3 'scrapy_mongodb.MongoDBPipeline': 300,4}
2 # 'www_dytt8_net.pipelines.WwwDytt8NetPipeline': 300,
3 'scrapy_mongodb.MongoDBPipeline': 300,
4}
然后写入mongodb配置即可
1MONGODB_URI = 'mongodb://localhost:27017'2MONGODB_DATABASE = 'spider_world'3MONGODB_COLLECTION = 'dytt8''mongodb://localhost:27017'
2MONGODB_DATABASE = 'spider_world'
3MONGODB_COLLECTION = 'dytt8'
来,咱们状态全开run一波,最后得到2W+部片子种子链接。
至此,我们便可以看到满满的电影种子链接,以后有什么想看的电影,自己直接在库里找找看,是不是很棒啊真正核心代码其实不到十行。以后不是很复杂的爬虫,还是用 Scrapy 吧。