当前位置: 首页 > article >正文

scrapy爬取汽车、车评数据【下】

1 情感分析管道

class SentimentPipeline:
    def __init__(self):
        self.config = Config()  #BERT的配置文件
        self.model = Model(self.config).to(self.config.device) #BERT模型导入

    def process_item(self, item, spider):
        # 可以添加保存数据库或文件的逻辑
        # print(item)
        if isinstance(item, CommentItem):
            print('进行情感分析...')
            GOLD = "\033[38;5;214m"  # 使用色号214表示金色
            RESET = "\033[0m"        # 重置颜色
            text = [item['content']]  # 这个需要根据实际情况修改(注意是一个数组)
            test_data = load_dataset(text, self.config)
            test_iter = build_iterator(test_data, self.config)
            result = final_predict(self.config, self.model, test_iter)
            for i, j in enumerate(result):
                item['label'] = j
                print(f"{GOLD}情感分析微博内容:{item['content']},结果:{j}{RESET}")
            print(item)
        if isinstance(item, DongchediItem):
            # print(item)
            pass
        return item

2 MySQL保存管道

class MySQLPipeline:
    def open_spider(self, spider):
        # 获取数据库配置
        self.connection = get_db_connection()
        self.cursor = self.connection.cursor()
        self.connection.commit()

    def close_spider(self, spider):
        # 关闭数据库连接
        self.cursor.close()
        self.connection.close()

    def process_item(self, item, spider):
        print('MySQL 管道...')
        PURPLE = "\033[95m"  # 紫色
        RESET = "\033[0m"  # 重置颜色
        if isinstance(item, DongchediItem):
            table = 'tb_car'
            insertlog = f"{PURPLE}汽车信息插入数据库成功:{item['car_name']}{RESET}"
        elif isinstance(item, SeriesItem):
            table = 'tb_series'
            insertlog = f"{PURPLE}车型评分插入数据库成功:{item['sid']}{RESET}"
        elif isinstance(item, CommentItem):
            table = 'tb_comment'
            insertlog = f"{PURPLE}评论插入数据库成功:{item['content']}{RESET}"
        else:
            return
        data = dict(item)
        keys = ', '.join(data.keys())
        values = ', '.join(['%s'] * len(data))
        sql = """INSERT INTO {table}({keys}) VALUES ({values}) ON
                                         DUPLICATE KEY UPDATE""".format(table=table,
                                                                        keys=keys,
                                                                        values=values)
        update = ','.join([" {key} = {key}".format(key=key) for key in data])
        sql += update
        try:
            self.cursor.execute(sql, tuple(data.values()))
            self.connection.commit()
            print(insertlog)
        except Exception as E:
            print('**********' + sql)
            print("Error:", E)
            self.connection.rollback()

        return item  # 必须返回 item

3 Settings 开启管道

ITEM_PIPELINES = {
   'car_spider.pipelines.SentimentPipeline': 300,
   'car_spider.pipelines.MySQLPipeline': 301
}

http://www.kler.cn/news/340805.html

相关文章:

  • 02. 上报自定义数据到 prometheus(使用 Python Client)
  • 【C++】set和multiset(关联式容器、键值对,set和multiset的基本特性、主要用途及常用操作)
  • 搜维尔科技:Haption远程操作项目模拟项目
  • Spring Validation —— 参数校验框架
  • Linux系统中,文件和文件夹的权限和所有权核心概念
  • Window系统编程 - 文件操作
  • 国庆档不太热,影视股“凉”了?
  • Win10之Ubuntu22.04(主机)与Virtual-BOX(宿主win10)网络互通调试(七十九)
  • k8s 中存储之 PV 持久卷 与 PVC 持久卷申请
  • 实现std::sort,replace,fill,accumulate,equal等函数
  • MyBatis之TypeHandler的自定义实现
  • Golang | Leetcode Golang题解之第462题最小操作次数使数组元素相等II
  • GNU/Linux - tarball文件介绍介绍
  • C#中Json序列化的进阶用法
  • Spring中注入bean时的scope属性详解、往singleton中注入prototype属性的bean以及Spring使用注解实现AOP切面编程
  • qwt实现码流柱状图多色柱体显示
  • SAP将假脱机(Spool requests)内容转换为PDF文档[RSTXPDFT4]
  • GAMES202作业3
  • 27-云计算下一个十年技术Serverless
  • 阿里140滑块-滑块验证码逆向分析思路学习