本项目爬取的是新片场的小视频,起始的url为: http://www.xinpianchang.com/channel/index/sort-like
总共有三层页面,分别是:
1、首页面,如下:
2、视频的详细页面,如下:
3、创作者的页面,如下:
该项目实现的就是把这三层页面中重要的信息都爬取下来,然后保存到mysql数据库,代码如下(呈现的主要是spider类中的代码):
import json import scrapy from scrapy import Request from xpc.items import PostItem,CommentItem,ComposerItem,CopyrightItem class DiscoverySpider(scrapy.Spider): name = 'xpc' allowed_domains = ['www.xinpianchang.com'] start_urls = ['http://www.xinpianchang.com/channel/index/sort-like'] # 提取作者的著作权信息 composer_url = 'http://www.xinpianchang.com/u%s?from=articleList' def parse(self, response): post_list = response.xpath('//ul[@class="video-list"]/li') #每个视频对应的第二层页面的链接 url = 'http://www.xinpianchang.com/a%s' #爬取第一层页面的信息,并且请求第二层的数据 for post in post_list: pid = post.xpath('./@data-articleid').extract_first() #请求第二层的页面,并且回调parse_post()函数对第二层的信息进行抓取 request = Request(url % pid, callback=self.parse_post) #提取每个视频的data-articleid request.meta['pid'] = pid #提取每个视频第一层页面的图片的链接 request.meta['thumbnail'] = post.xpath('.//img/@_src').get() #提取每个视频的描述 request.meta['duration'] = post.xpath('./a/span/text()').get()