You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from typing import List
import time
import mysql
import mysql.connector
import csv
def hello():
import quchong
quchong.qu()
db_config = {
'host': 'localhost',
'user': 'root',
'password': '12345678M',
'database': 'pinglun',
}
cnx = mysql.connector.connect(**db_config)
cursor = cnx.cursor()
# 读取CSV
with open('抖音评论.csv', mode='r', encoding='utf-8') as file:
reader = csv.reader(file)
headers = next(reader, None)
if headers is not None: # 过表头
for row in reader:
sql = "INSERT IGNORE INTO douyinpinglunxinxi(nicheng,zhanghao,pinglun) VALUES (%s, %s,%s)"
cursor.execute(sql, (row[0], row[1],row[2])) #设csv与数据库对应
try:
cnx.commit()
print("数据导入成功!")
except mysql.connector.Error as err:
print(f"数据导入失败: {err}")
try:
cursor.execute("SELECT zhanghao FROM douyinpinglunxinxi")
zhanghao_list = cursor.fetchall()
print("从数据库读取的 zhanghao")
for zhanghao in zhanghao_list:
import zidongcaozuo
zidongcaozuo.zidong(zhanghao[0])
time.sleep(30)
except mysql.connector.Error as err:
print(f"读取 zhanghao 失败: {err}")
finally:
cursor.close()
cnx.close()
hello()