基于python的-爬取糗事百科(工具类)

# -*- coding:utf-8 -*-
import re
import sqlite3
class Tools(object):

    @classmethod
    def strip_char(cls, string):
        '''

        :param string: 要进行处理的数据
        :return: 处理之后的数据
        '''
        # 利用正则去除特殊字符
        string = re.sub(re.compile('\n|\t| ',re.S),'',string)
        # 将换行标签替换为\n
        string = re.sub(re.compile('<br/>'),'\n',string)
        return string

# 数据库管理类
class DBManager(object):

    # 声明类变量
    connect = None
    cursor = None
    # 连接数据库
    @classmethod
    def connect_db(cls):
        cls.connect = sqlite3.connect('qsbk.db')
        cls.cursor = cls.connect.cursor()

    @classmethod
    def close_db(cls):
        cls.cursor.close()
        cls.connect.close()

    @classmethod
    def insert_data(cls, dz_tuple):
        sql = "INSERT INTO qsbk(images,name,age,content,smile,speak) VALUES ('%s','%s',%s,'%s',%s,%s)" % (dz_tuple[0], dz_tuple[1], dz_tuple[2], dz_tuple[3], dz_tuple[4], dz_tuple[5])
        cls.cursor.execute(sql)
        cls.connect.commit()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值