栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

使用scrapy爬取新片场的数据

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用scrapy爬取新片场的数据

爬取新片场的数据

一:
https://www.xinpianchang.com/channel/index/sort-like?from=navigator
视频id。、视频标题。、视频缩略图。、视频分类。、视频创建时间。、视频详情链接。
二:
视频详情:
视频id、视频预览图。、视频链接。、视频格式。、作品分类。、播放时长。、作品描述。、播放次数。、被点赞次数。
三:
作者详情:
作者id。、作者主页banner图片。、用户头像。、作者名字。、自我介绍。,被点赞次数。、粉丝数量。、关注数量。、所在位置、职业。
四:
评论数据:
评论id。、评论作品id(视频id)。、评论人id。、评论人名称。、发表时间。、评论内容。、被点赞次数。、

爬取上面的数据存储到mongodb数据库中
其中每个数据对应着一个表

网站链接

解析这个页面,获取需求的数据,

可以发现每一个li标签中
通过xpath可以定位到这里 /html/body/div[7]/div[2]/ul/li

import scrapy

from xinpianchang.items import *  # 导入items里面所有的建模类
import re
import time
import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='gb18030')
# 修改输出格式,有些情况下print无法输出特殊字符,需要修改输出格式,可以不设置
class NodeXinpianchangSpider(scrapy.Spider):
    name = 'node_xinpianchang'
    allowed_domains = ['xinpianchang.com']
    start_urls = ['https://www.xinpianchang.com/channel/index/sort-like?from=navigator']


    # 初始页面解析
    def parse(self, response,**kwargs):
        temps = response.xpath("/html/body/div[7]/div[2]/ul/li[1]")
        # 因为数据太多先拿第一个视频数据经行解析
        #初始页面可以提取如下的数据
            for temp in temps:
            # 视频标题
            tag_name  = temp.xpath("./div/div[1]/a/p/text()").extract_first()
            # print(tag_name)

            # 视频时长
            tag_time= temp.xpath("./a/span/text()").extract_first()
            # print(tag_time)

            # 视频缩略图
            name_picture = temp.css(".lazy-img::attr(_src)").extract_first()
            # print(name_picture)

            # 视频id
            tag_id = temp.css(".enter-filmplay::attr(data-articleid)").extract_first()
            # print(tag_id)

            # 视频详情链接
            tag_url = f"https://www.xinpianchang.com/a{tag_id}?from=ArticleList"
            # print(tag_url)

            # 视频分类
            tag_title = []
            tag_titles = temp.xpath("./div/div[1]/div[1]/span/text()").extract()
            for tag in tag_titles:
                tag_title.append(tag.strip())

            # print(tag_title)

            # 播放量
            tag_play = temp.xpath("./div/div[1]/div[2]/span[1]/text()").extract_first()

            # 点赞量
            tag_give = temp.xpath("./div/div[1]/div[2]/span[2]/text()").extract_first()
	
			# 请求详情链接、使用callback回调、用meta将数据传出去
            yield scrapy.Request(
                url=tag_url,
                callback=self.parse_detail,
                meta={
                    "tag_id":tag_id,
                    "tag_title":tag_title,
                    "tag_play":tag_play,
                    "tag_give":tag_give,
                    "tag_time":tag_time,
                    "tag_name":tag_name,
                    "name_picture":name_picture,
                    "tag_url":tag_url,

                }
            )
            

在items里面经行建模将数据放到建模中
将所有所需要的数据经行建模
写在items.py里面

# coding=utf-8
# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html

import scrapy


class Xinpianchang01Item(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()

    mongodb_collection = "The_initial_video"

    # """
    # 视频id。、视频标题。、视频缩略图。、视频分类。、视频创建时间。、视频详情链接。
    # """
    video_name = scrapy.Field()   # 视频标题
    video_picture = scrapy.Field()  # 视频缩略图
    video_id = scrapy.Field()   # 视频id
    video_url = scrapy.Field()   # 视频详情链接
    video_title = scrapy.Field()  # 视频分类
    setup_time = scrapy.Field()  # 视频创建时间




class VideoItem(scrapy.Item):

    # """
    # 视频id、视频预览图。、视频链接。、视频格式。、作品分类。、播放时长。、作品描述。、播放次数。、被点赞次数
    # """
    # # 视频详情

    mongodb_collection = "video_data"

    video_time = scrapy.Field()  # 视频时长
    video_play = scrapy.Field()  # 播放量
    video_give = scrapy.Field()  # 点赞量
    # 视频id
    video_id = scrapy.Field()
    # 作品分类
    video_title = scrapy.Field()
    # 视频预览图
    video_img = scrapy.Field()
    # 作品描述
    CDWA = scrapy.Field()
    # 视频链接
    video_url = scrapy.Field()
    # 视频格式
    format = scrapy.Field()


class AuthorItem(scrapy.Item):

    # """
    # 作者id。、作者主页banner图片。、用户头像。、作者名字。、自我介绍。,被点赞次数。、粉丝数量。、关注数量。、所在位置、职业。
    # """
    # 作者详情
    mongodb_collection = "Author"

    # 作者id
    author_id = scrapy.Field()
    # 作者名称
    author_name = scrapy.Field()
    # 作者头像
    author_head = scrapy.Field()
    # 作者主页banner图片
    banner = scrapy.Field()
    # 自我介绍
    introduce = scrapy.Field()
    # 被点赞次数
    by_gives = scrapy.Field()
    # 粉丝数量
    count = scrapy.Field()
    # 关注数量
    attention = scrapy.Field()
    # 所在位置
    address = scrapy.Field()
    # 职业
    job = scrapy.Field()

    pass

class CommentItem(scrapy.Item):
    # 评论详情
    # """
    # 评论id。、评论作品id(视频id)。、评论人id。、评论人名称。、发表时间。、评论内容。、被点赞次数。、
    # """

    mongodb_collection = "Comment"
    # 评论内容
    content = scrapy.Field()

    # 评论作品id(视频id)
    resource_id = scrapy.Field()

    # 评论id
    id = scrapy.Field()

    # 评论人id
    userid = scrapy.Field()

    # 评论人名称
    username = scrapy.Field()

    # 被点赞次数
    count_approve = scrapy.Field()

    # 发表时间
    addtime = scrapy.Field()

    pass



回调的方法 parse_detail 会来到这个页面

解析这个页面

# 加载的视频页面解析
    def parse_detail(self,response):
        # 视频id
        tag_id = response.meta["tag_id"]
        # 视频名称
        tag_name = response.meta["tag_name"]
        # 视频缩略图
        name_picture = response.meta["name_picture"]
        # 视频详情链接
        tag_url = response.meta["tag_url"]
        # 作品分类
        tag_title = response.meta["tag_title"]

通过解析会发现一些数据是异步加载的
找到这个链接
https://mod-api.xinpianchang.com/mod/api/v2/media/9Mnp8wWKkBKwK0lG?appKey=61a2f329348b3bf77&extend=userInfo%2CuserStatus
可以发现着里面藏着视频的mp4链接

在解析链接可以发现链接后面的&extend=userInfo%2CuserStatus 可以不需要,也可以请求链接

https://mod-api.xinpianchang.com/mod/api/v2/media/9Mnp8wWKkBKwK0lG?appKey=61a2f329348b3bf77

这个就是要请求的链接

两个参数在哪里呢?
9Mnp8wWKkBKwK0lG?
appKey=61a2f329348b3bf77
在源码里面


通过搜索可以发现 9Mnp8wWKkBKwK0lG 在 a标签中
通过xpath可以直接提取参数 /html/body/div[7]/div[2]/div[1]/div[1]/div[1]/div[4]/div[2]/span[2]/span[1]/a/@data-vid


再次通过搜索可以发现 61a2f329348b3bf77 这个参数
因为这个没有标签
所有通过re正则表达式获取数据 var.appKey.=."(.*?)"

这样就拿到了这些参数
如何只需要将他们合并成链接就可以了

f"https://mod-api.xinpianchang.com/mod/api/v2/media/{node_str}?appKey={node_id[0]}"
        # 视频加密参数
        ## id
        node_id = response.xpath(".").re('var.appKey.=."(.*?)"')
        # print(node_id)

        ## str
        node_str = response.xpath("/html/body/div[7]/div[2]/div[1]/div[1]/div[1]/div[4]/div[2]/span[2]/span[1]/a/@data-vid").extract_first()
        # print(node_str)

        # https://mod-api.xinpianchang.com/mod/api/v2/media/9Mnp8wWKkBKwK0lG?appKey=61a2f329348b3bf77

        # 提取视频链接的url
        url = f"https://mod-api.xinpianchang.com/mod/api/v2/media/{node_str}?appKey={node_id[0]}"
        # print(url)

        # 视频创建时间
        setup_time = response.xpath(
            "/html/body/div[7]/div[2]/div[1]/div[1]/div[1]/div[3]/span[2]/i/text()").extract_first()
        # print(setup_time)

		# 第一个表所需要的数据全部找全了,将同他们放进建模中
        yield Xinpianchang01Item(
            setup_time=setup_time,
            video_name=tag_name,
            video_id=tag_id,
            video_picture=name_picture,
            video_url=tag_url,
            video_title=tag_title,
        )


        # 播放次数
        tag_play = response.meta["tag_play"]

        # 被点赞次数
        tag_give = response.meta["tag_give"]

        # 视频时长
        tag_time = response.meta["tag_time"]

        # 视频预览图
        name_img = response.xpath('/html/head/meta[11]/@content').extract_first()
        # print(name_img)



        # 作品描述
        CDWA = []
        abouts = response.xpath("/html/body/div[7]/div[2]/div[1]/div[1]/div[2]/p/text()").extract()
        for about in abouts:
            pass
            CDWA.append(about.strip())

        yield scrapy.Request(
            url=url,
            callback=self.video_detail,
            meta={
                "tag_id": tag_id,
                "tag_time": tag_time,
                "tag_title": tag_title,
                "tag_play": tag_play,
                "tag_give": tag_give,
                "name_img": name_img,
                "CDWA":CDWA

            }

        )

        temps = response.xpath("/html/body/div[7]/div[2]/div[2]/div[1]/div[1]/ul/li")
        for temp in temps:
            # 作者的主页链接
            author_url = f'https://www.xinpianchang.com{temp.xpath("./a/@href").extract_first()}'

            # 作者id
            author_id = temp.xpath("./div[2]/@data-userid").extract_first()
            # print(author_id)

            # 作者名称
            author_name = temp.xpath("./div[1]/a/span/span/text()").extract_first()
            # print(author_name)

            # 作者头像
            author_head = temp.xpath("./a/img/@_src").extract_first()
            # print(author_head)

            yield scrapy.Request(
                url=author_url,
                callback=self.author_detail,
                meta={
                    "author_id":author_id,
                    "author_name":author_name,
                    "author_head":author_head
                }
            )



再次查看异步加载的请求可以发现评论的链接

https://app2.xinpianchang.com/comments?resource_id=11914087&type=article&page=1&per_page=24&_=1654054326985

解析链接可以发现

&_=1654054326985 也可以不需要

还可以发展链接可以设置最大值
设置获取最大的评论数据
per_page=24
将它设置为最大获取100个评论
这个链接则会最大获取100评论,少于100个评论的则全部获取

resource_id=11914087 在里面的参数着是视频的id 上面已经获取到了

这样就解析完成了全部评论的链接
最后回调评论的解析方法

        # 评论url
        comment_url = f"https://app2.xinpianchang.com/comments?resource_id={tag_id}&type=article&page=1&per_page=100"

        yield scrapy.Request(
            url=comment_url,
            callback=self.comment_detail
        )

这个链接请求的是json数据


在里面提取1数据

#解析评论
    def comment_detail(self,response):
        temp = response.json()
        comments = temp["data"]["list"]
        # print(comment)
        for comment in comments:
            # 评论内容
            content = comment["content"]
            # 使用正则去除
content = re.sub("
","",content) # print(content) # 评论作品id(视频id) resource_id = comment["resource_id"] # print(resource_id) # 评论id id = comment["id"] # print(id) # 评论人id userid = comment["userid"] # print(userid) # 评论人名称 username = comment["userInfo"]["username"] # print(username) # 被点赞次数 count_approve = comment["count_approve"] # print(count_approve) # 发表时间 是时间戳需要处理一下 addtimes = comment["addtime"] # 是时间戳 time_1 = addtimes time_tuple_1 = time.localtime(time_1) addtime = time.strftime("%Y/%m/%d %H:%M:%S", time_tuple_1) # print(addtime) yield CommentItem( content=content, resource_id=resource_id, id=id, userid=userid, username=username, count_approve=count_approve, addtime=addtime )

解析作者的首页

# 作者主页页面解析
    def author_detail(self,response):

        author_id = response.meta["author_id"]
        author_name = response.meta["author_name"]
        author_head = response.meta["author_head"]

        # 作者主页banner图片url
        banners = response.xpath('/*;q=0.8',
#   'Accept-Language': 'en',
#}

# Enable or disable spider middlewares
# See https://docs.scrapy.org/en/latest/topics/spider-middleware.html
# 调用ua头,IP关闭了,需要的自己取消注释
SPIDER_MIDDLEWARES = {
   'xinpianchang.middlewares.RandomUserAgentMiddleware': 543,
   # 'xinpianchang.middlewares.RandomProxyMiddleware': 544,
}

# Enable or disable downloader middlewares
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
# DOWNLOADER_MIDDLEWARES = {
#    'xinpianchang.middlewares.XinpianchangDownloaderMiddleware': 543,
# }

# Enable or disable extensions
# See https://docs.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {
#    'scrapy.extensions.telnet.TelnetConsole': None,
#}

# Configure item pipelines
# See https://docs.scrapy.org/en/latest/topics/item-pipeline.html

# 开启项目管道
ITEM_PIPELINES = {
   'xinpianchang.pipelines.XinpianchangPipeline': 300,
   'xinpianchang.pipelines.ImagePipeline': 301,
}

# Enable and configure the AutoThrottle extension (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED = True
# The initial download delay
#AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False

# Enable and configure HTTP caching (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/942244.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号