山东大学创新实训2020/6/7

山东大学项目实训

今天完成了数据库的写入
主要使用了mysql数据库
创建数据库
在这里插入图片描述

再flak完成数据库的接入

from crawler import toutiao
import pymysql

db = pymysql.connect(host="localhost", port=3308,user="root", passwd="",database="toutiao")
cursor = db.cursor()


def  test():
    # query = input('请输入想要查询的东西')
    query = '特朗普'
    toutiaocrawler = toutiao.toutiaocrawler()
    # # cookie = input('请输入cookie')
    # # toutiaocrawler.set_cookie(cookie)
    all_list = toutiaocrawler.main(query)
    try:
        cursor.execute('''DROP TABLE IF EXISTS toutiao''')
        cursor.execute('''create table IF NOT EXISTS  toutiao (id INT PRIMARY KEY AUTO_INCREMENT,content  Text not null ,comment_time Text not null,comment Text )''')
        cursor.execute('''alter table toutiao convert to character set utf8''')
        for i in all_list:
            try:
                if len(i[2]) == 0:
                    sql = "INSERT INTO toutiao(content,comment_time,comment) VALUES ('%s', '%s', '%s')" % (i[0], i[1], "")
                    cursor.execute(sql)
                else:
                    for z in i[2]:
                        sql = "INSERT INTO toutiao(content,comment_time,comment) VALUES ('%s', '%s', '%s')" % (i[0], i[1], z)
                        cursor.execute(sql)
            except:
                continue
    except:
        print("...mysql error")
        db.rollback()


    db.close()

在这里插入图片描述

实现实时爬取并存放到数据库中

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值