1. 安装Python链接MySQL插件。

$ pip3 install PyMySQL

2. 完整代码如下:

import os
import pymysql

sku = []

#定义递归函数findPNG,寻找当前目录下所有png类型的文件
def findPNG(sku,dir):
    for ffName in os.listdir(dir):
        if os.path.isfile(dir+"/"+ffName):
            if ffName.split(".")[1] == "png":
                sku.append(ffName.split(".")[0]);
        elif os.path.isdir(dir+"/"+ffName):
            findPNG(sku,dir+"/"+ffName)
findPNG(sku,"./")

h = open("./ProductsInformation.csv","a")
db = pymysql.connect("地址","用户","密码","数据库名" )
cursor = db.cursor()

for s in sku:
    SQL = '''SELECT t1.model,t2.name,t2.tag
             FROM `product` AS t1, `product_description` AS t2
             WHERE t1.product_id = t2.product_id AND t1.model ="{}"'''.format(s)
    cursor.execute(SQL)
    data = cursor.fetchone()
    if data:
        for d in data:
            h.write('"'+d+'",')
        h.write('\n')
    else:
        print(s)

h.close()
db.close()

发表评论

电子邮件地址不会被公开。 必填项已用*标注