使用scrapy抓取Youtube播放列表信息

可参看Knowsmore

抓取Youtube列表数据的前提是scrapy部署的机器可以正常访问Youtube网站

示例网址

存取到Mongo中的数据如下:

{
    "playlist_id" : "PLEbPmOCXPYV67l45xFBdmodrPkhzuwSe9",
    "videos" : [
        {
            "playlist_id" : "PLEbPmOCXPYV67l45xFBdmodrPkhzuwSe9",
            "video_id" : "9pTwztLOvj4",
            "thumbnail" : [
                {
                    "url" : "https://i.ytimg.com/vi/9pTwztLOvj4/hqdefault.jpg?sqp=-oaymwEZCPYBEIoBSFXyq4qpAwsIARUAAIhCGAFwAQ==&rs=AOn4CLCmUXUPe-HgXiie0SRfL5cYz0JRrg",
                    "width" : 245,
                    "height" : 137
                }
            ],
            "title" : "Legend of the galactic heroes (1988) episode 1",
            "index" : 1,
            "length_seconds" : 1445,
            "is_playable" : true
        },
        {
            "playlist_id" : "PLEbPmOCXPYV67l45xFBdmodrPkhzuwSe9",
            "video_id" : "zzD1xU37Vtc",
            "thumbnail" : [
                {
                    "url" : "https://i.ytimg.com/vi/zzD1xU37Vtc/hqdefault.jpg?sqp=-oaymwEZCPYBEIoBSFXyq4qpAwsIARUAAIhCGAFwAQ==&rs=AOn4CLCnLCYaZVBeHnZR0T73rfEd_Dbyew",
                    "width" : 245,
                    "height" : 137
                }
            ],
            "title" : "Legend of the galactic heroes (1988) episode 2",
            "index" : 2,
            "length_seconds" : 1447,
            "is_playable" : true
        },

代码如下:

# -*- coding: utf-8 -*-
import scrapy
import re
import json
from scrapy import Selector
from knowsmore.items import YoutubePlaylistItem, YoutubePlaylistVideoItem
from ..common import *

class YoutubeListSpider(scrapy.Spider):
    name = 'youtube_list'
    allowed_domains = ['www.youtube.com']
    start_urls = ['https://www.youtube.com/playlist?list=PLEbPmOCXPYV67l45xFBdmodrPkhzuwSe9']

    def parse(self, response):
        # Extract JSON Data with Regex Expression
        ytInitialData = r1(r'window\["ytInitialData"\] = (.*?)}};', response.body)
        if ytInitialData:
            ytInitialData = '%s}}' % ytInitialData
            ytInitialDataObj = json.loads(ytInitialData)

            # Assign VideoList info to variable
            playListInfo = ytInitialDataObj['contents']['twoColumnBrowseResultsRenderer']['tabs'][0]['tabRenderer']['content']['sectionListRenderer']['contents'][0]['itemSectionRenderer']['contents'][0]['playlistVideoListRenderer']

            # Build Scrapy Item
            playList = YoutubePlaylistItem(
                playlist_id = playListInfo['playlistId'],
                videos = []
            )

            # Insert the videoItem to YoutubePlaylistItem videos field
            for videoInfo in playListInfo['contents']:
                videoInfo = videoInfo['playlistVideoRenderer']
                videoItem = YoutubePlaylistVideoItem(
                    playlist_id = playListInfo['playlistId'],
                    video_id = videoInfo['videoId'],
                    thumbnail = videoInfo['thumbnail']['thumbnails'],
                    title = videoInfo['title']['simpleText'],
                    index = videoInfo['index']['simpleText'],
                    length_seconds = videoInfo['lengthSeconds'],
                    is_playable = videoInfo['isPlayable']
                )
                playList['videos'].append(videoItem)
            
            yield playList
            
        
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值