使用scrapy抓取Youtube播放页数据
可参看Knowsmore
抓取Youtube播放页数据的前提是scrapy部署的机器可以正常访问Youtube网站
抓取的原理是读取Youtube播放页桌面版网页源代码中的全局变量: ytInitialData
存取到Mongo中的数据如下:
{ "title" : "20130410 锵锵三人行 陈平原谈中国教育问题", "view_count" : "12,407 views" }
代码如下:
# -*- coding: utf-8 -*- import scrapy import re import json from scrapy import Selector from knowsmore.items import YoutubeItem from ..common import * class YoutubeSpider(scrapy.Spider): custom_settings = { 'DOWNLOADER_MIDDLEWARES' : { } } name = 'youtube' allowed_domains = ['www.youtube.com'] start_urls = ['https://www.youtube.com/watch?v=3vkqOdMBP48'] def parse(self, response): ytInitialData = r1(r'ytInitialData"\] = (.*?)}};', response.body) if ytInitialData: ytInitialData = '%s}}' % ytInitialData ytInitialDataObj = json.loads(ytInitialData) videoInfo = ytInitialDataObj['contents']['twoColumnWatchNextResults']['results']['results']['contents'][0]['videoPrimaryInfoRenderer'] Item = YoutubeItem( title = videoInfo['title']['simpleText'].encode('utf-8'), view_count = videoInfo['viewCount']['videoViewCountRenderer']['viewCount']['simpleText'] ) yield Item
相关推荐
Hesland 2020-04-20
ForEachkaka 2019-11-04
zhbbupt 2019-09-23
bjzhangfei 2019-07-05
vitavae 2019-07-01
ksjlhy 2019-06-29
moshlwx 2019-02-03
simplehap 2017-09-01
hanyanqing 2017-08-28
yimixgg 2018-08-14
dbhllnr 2017-07-09
qinrui 2017-05-04
Sigh 2017-03-24
YannZoe 2018-09-17
windgoogle 2017-08-18
knijiokm 2011-11-22
新地址zhanglin 2018-10-15
linuxisperfect 2011-02-09