Scrapy+Mysql实现的一个小项目,爬取新片场中的小视频的内容

本项目爬取的是新片场的小视频,起始的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()
            
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值