博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
教你利用Scrapy框架全站爬取视频资源信息
阅读量:2070 次
发布时间:2019-04-29

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

1、先在settings.py把配置配好

# 关闭ROBOTS协议ROBOTSTXT_OBEY = False# 设置log 日志等级LOG_LEVEL = 'WARNING'# 开启管道ITEM_PIPELINES = {   'mini_resource_scrapy.pipelines.MiniResourceScrapyPipeline': 300,}# 设置USER_AGENT 这个直接打开浏览器,F12控制台随便找个请求,请求头信息里面有USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"

2、在items.py文件中定义数据模型,把要爬的字段在这里定义,我这里贴代码给大家参考 

import scrapyclass MiniResourceScrapyItem(scrapy.Item):    # define the fields for your item here like:    # name = scrapy.Field()    _id = scrapy.Field()       # 数据id    add_date = scrapy.Field()  # 视频添加的时间    title = scrapy.Field()     # 标题    director = scrapy.Field()   # 导演    stars = scrapy.Field()  # 主演,这里我存数组    type = scrapy.Field()  # 电影类型 这里我存数组    area = scrapy.Field()  # 电影地区    year = scrapy.Field()  # 电影年份    describe = scrapy.Field()  #电影描述    source = scrapy.Field()   # 视频来源,(这里我都写目前爬取的网站地址)    video_url = scrapy.Field() # 视频播放url地址    cover_img = scrapy.Field()  # 视频封面url 地址    score = scrapy.Field()     # 电影评分 (我爬的网站没有拿到评分信息,后面都给初始值 0 )    play_count = scrapy.Field() # 视频播放次数 (给初始值 0)    is_show = scrapy.Field()  #  前端是否展示    is_new = scrapy.Field()  # 是否是最新

3、配置管道pipelines.py, 用于把爬取来的数据存起来,存文件活着数据库这里随你,这里我直接存文件

import codecs,json# 用codecs提供的open方法来指定打开的文件的语言编码,它会在读 取的时候自动转换为内部unicode class MiniResourceScrapyPipeline(object):    def __init__(self):        self.file = codecs.open('movie.json', 'w', encoding='utf-8')    # 在open_spider 方法中打开文件也可以,爬虫启动的时候,文件之打开一次,爬虫结束的时候关闭文件    # def open_spider(self, spider):    #     pass    # 在文件里面存入json 数据    def process_item(self, item, spider):        lines = json.dumps(dict(item), ensure_ascii=False) + "\n"        self.file.write(lines)        return item    # 爬虫结束,关闭文件    def spider_closed(self, spider):        self.file.close()

4、正式开始我们的爬虫代码 爬的是  这个视频网站,大家也可以用别的网站爬,xpath要写正确啊,逻辑都一样

import scrapyfrom ..items import MiniResourceScrapyItemimport reclass Mv80Spider(scrapy.Spider):    name = 'mv80'    total_page = 4 # 这边我只爬取3页数据     page_index = 1 # 从第一页开始    origin = 'https://www.80mv.cc'    id_compile = re.compile(r'80detail_(\d+).html')    r_compile = re.compile(r'https:(.+)/index.m3u8', re.I)    # allowed_domains = ['www.80mv.cc']    start_urls = ['https://www.80mv.cc/80show_dianying--------{}---.html']        # 设置爬取的开始url地址    def start_requests(self):        for i in range(self.page_index, self.total_page):            yield scrapy.Request(url=self.start_urls[0].format(i))        def parse(self, response):        lis = response.xpath('//ul[contains(@class,"stui-vodlist clearfix")]/li')        for tag in lis:            page_url = tag.xpath('.//a[@class="stui-vodlist__thumb lazyload"]/@href').extract_first()            _id = self.id_compile.search(page_url).groups()[0]            # 电影标题            title = tag.xpath('.//a[@class="stui-vodlist__thumb lazyload"]/@title').extract_first()            # 电影封面图            cover_img = tag.xpath('.//a[@class="stui-vodlist__thumb lazyload"]/@data-original').extract_first()            # 电影详情内页地址            detail_url = tag.xpath('.//a[@class="stui-vodlist__thumb lazyload"]/@href').extract_first()            # print(title, cover_img)            # 实例化数据模型 把数据存入            item = MiniResourceScrapyItem()            item['title'] = title            item['cover_img'] = cover_img            item['_id'] = _id            # 进入详情页继续爬取详情信息            yield scrapy.Request(                url='{}{}'.format(self.origin, detail_url),                callback=self.video_detail,                meta={'item': item})    # 视频详情页    def video_detail(self, response):        item = response.meta['item']        des = response.xpath('//div[@class="stui-content__detail"]')        stars = des.xpath('./p[1]//a//text()').extract()        director = des.xpath('./p[2]/a/text()').extract_first()        describe = des.xpath('./p[5]/span[2]/text()').extract_first()        m_type = des.xpath('string(./p[3]/span[contains(text(), "类型")]/following-sibling::text())')\            .extract_first().strip().split(',')        area = des.xpath('string(./p[3]/span[contains(text(), "地区")]/following-sibling::text())')\            .extract_first().strip()        year = des.xpath('string(./p[3]/span[contains(text(), "年份")]/following-sibling::text())')\            .extract_first().strip()        add_date = des.xpath('string(./p[4]/span[contains(text(), "时间")]/following-sibling::text())') \            .extract_first().strip()        video_page_url = response.xpath('//ul[@class="stui-content__playlist clearfix"]/li/a/@href').extract_first()        video_page_url = '{}{}'.format(self.origin, video_page_url)        item['stars'] = stars        item['type'] = m_type  # 类型        item['area'] = area  # 区域        item['year'] = year  # 年代        item['director'] = director  # 导演        item['describe'] = describe  # 描述        item['score'] = 0  # 没有电影评分,给个初始值        item['play_count'] = 0  # 点击次数 给个初始值        item['is_show'] = True  # 是否展示        item['is_new'] = True if year == '2021' else False  # 是否是最新        item['add_date'] = add_date        item['source'] = '80S影院'        yield scrapy.Request(url=video_page_url, callback=self.video_play_page, meta={'item': item})        # 视频播放页    def video_play_page(self, response):        item = response.meta['item']        script_str = response.xpath('//div[@class="stui-player__video clearfix"]/script[1]/text()').extract_first()        g_url = self.r_compile.search(script_str)        video_url = g_url.group().replace('\\', '')        item['video_url'] = video_url        print(item)        yield item

 5、这是我爬的数据

代码结束,文字很少,全是代码,如果有不明白的地方可以留言,大家相互学习讨论

!!!!!!下面是我自己的小程序,请大家多多扫码打开给点支持吧!!!!

转载地址:http://wqsmf.baihongyu.com/

你可能感兴趣的文章
Oracle Spatial中的Operator操作子 详细说明
查看>>
Oracle Spatial中SDO_Geometry详细说明
查看>>
oracle 聚合函数 LISTAGG ,将多行结果合并成一行
查看>>
Oracle列转行函数 Listagg() 语法详解及应用实例
查看>>
LISTAGG函数的用法
查看>>
Oracle Spatial操作geometry方法
查看>>
IDEA类和方法注释模板设置(非常详细)
查看>>
Java程序初始化的顺序
查看>>
Dubbo和Spring结合配置文件内容解析为bean的过程
查看>>
fastJson注解@JSONField使用的一个实例
查看>>
fastjson的@JSONField注解的一点问题
查看>>
fastjson使用(三) -- 序列化
查看>>
浅谈使用单元素的枚举类型实现单例模式
查看>>
Java 利用枚举实现单例模式
查看>>
Java 动态代理作用是什么?
查看>>
Java动态代理机制详解(JDK 和CGLIB,Javassist,ASM) (清晰,浅显)
查看>>
三种线程安全的单例模式
查看>>
Spring AOP 和 动态代理技术
查看>>
从 volatile 说起,可见性和有序性是什么
查看>>
如何开始接手一个项目
查看>>