scrapy 豆瓣短评 数据分析 + 中文情感分析 + 可视化 (一)

scrapy 豆瓣短评 数据分析 + 中文情感分析 + 可视化 (一)

一、scrapy 爬取 豆瓣短评
本次爬取的是哪吒之魔童降世 短评 。本次爬取的是静态网页还是蛮简单的。
1、开始地址

https://movie.douban.com/subject/26794435/comments?status=P

爬取的内容
在这里插入图片描述

item设置为

class DoubanscrapyItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    table = 'douban'
    name = scrapy.Field()
    grade=scrapy.Field()
    content=scrapy.Field()
    time=scrapy.Field()
    support_num=scrapy.Field()
    pass

爬取
spider 为
在这里插入图片描述

class DoubanSpider(scrapy.Spider):
    name = 'douban'

    allowed_domains = ['movie.douban.com']
    start_urls = ['https://movie.douban.com/subject/26794435/comments?status=P']

    def __init__(self):
        self.start_url='https://movie.douban.com/subject/26794435/comments?status=P'
        self.next_url='https://movie.douban.com/subject/26794435/comments{next}'


    def start_requests(self):
        yield scrapy.Request(self.start_url, callback=self.get_parse)

    def get_parse(self, response):
        #print(response.body.decode('utf-8'))

        contexts=response.xpath('//*[@class="comment-item"]')

        for context  in contexts :
            item=DoubanscrapyItem()
            item["name"] = context.xpath(".//@title").extract_first()
            item["grade"] = context.xpath('.//*[@class="comment-info"]// span[2]/@title').extract_first()
            item["time"] = context.xpath('.//*[@class="comment-info"]//*[@class="comment-time "]/@title').extract_first()
            item["content"] = context.xpath('.//*[@class="short"]/text()').extract_first()
            item["support_num"]= context.xpath('.//*[@class="votes"]/text()').extract_first()
            yield  item

        next_page= context.xpath('//*[@id="paginator"]//*[@class="next"]/@href').extract_first()
        if next_page is not None :
            next= self.next_url.format(next=next_page)

            yield scrapy.Request(next, callback=self.get_parse)

数据库的存储

   
class MysqlPipeline():
    def __init__(self, host, database, user, password, port):
        self.host = host
        self.database = database
        self.user = user
        self.password = password
        self.port = port

    @classmethod
    def from_crawler(cls, crawler):
        return cls(
            host=crawler.settings.get('MYSQL_HOST'),
            database=crawler.settings.get('MYSQL_DATABASE'),
            user=crawler.settings.get('MYSQL_USER'),
            password=crawler.settings.get('MYSQL_PASSWORD'),
            port=crawler.settings.get('MYSQL_PORT'),
        )

    def open_spider(self, spider):
        self.db = pymysql.connect(self.host, self.user, self.password, self.database, charset='utf8',
                                  port=self.port)
        self.cursor = self.db.cursor()

    def close_spider(self, spider):
        self.db.close()

    def process_item(self, item, spider):
        data = dict(item)
        print(data)
        keys = ', '.join(data.keys())
        values = ', '.join(['%s'] * len(data))
        sql = 'insert into %s (%s) values (%s)' % (item.table, keys, values)
        self.cursor.execute(sql, tuple(data.values()))
        self.db.commit()
        return item



爬取的数据如下
在这里插入图片描述

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值