前言
这一次也是爬取数据
一、爬取的数据
test5
共有4933条
二、数据处理的源码
import csv
import pymysql
def clean():
with open("D:/test4.csv", "r", encoding = "utf-8") as f:
reader = csv.reader(f)
rows = [row for row in reader]
info=[]
#print(rows)
for i in range(1,len(rows)):
#print(rows[i])
rol=[]
rol.append(i)
rol.append("huawei")
rol.append(int(rows[i][7][:-1]))
rol.append("无")
rol.append(rows[i][1])
if ',' in rows[i][11]:
if rows[i][11][1]==',':
price = int(rows[i][11][0])*1000+int(float(rows[i][11][2:]))
elif rows[i][11][2]==',':
price = int(rows[i][11][0:2]) * 1000 + int(float(rows[i][11][3:]))
elif rows[i][11][3]==',':
price = int(rows[i][11][0:3]) * 1000 + int(float(rows[i][11][4:]))
elif rows[i][11]=='':
price=0
else: price=int(float(rows[i][11]))
rol.append(price)
#print(price)
rol.append(1)
rol.append("无")
rol.append(rows[i][6])
rol.append(int(rows[i][8][:-2]))
rol.append("2021-07-08")
info.append(rol)
return info
info=clean()
print(info[0])
db = pymysql.connect(host="", user="",
password="", database="")
cur = db.cursor()
for i in range(len(info)):
sql = "INSERT INTO huawei_product VALUES ({},'{}',{},'{}','{}',{},{},'{}','{}',{},'{}')"\
.format(info[i][0],info[i][1],info[i][2],info[i][3],info[i][4],info[i][5],info[i][6],info[i][7],info[i][8],info[i][9],info[i][10])
try:
# 执行sql语句
cur.execute(sql)
# 提交到数据库执行
db.commit()
print("成功",i)
except:
# Rollback in case there is any error
db.rollback()
print("失败",i)