从数据库中获取图片编号,然后通过request获取图片下载

import pandas as pd
from pandas.core.dtypes.dtypes import register_extension_dtype
from sqlalchemy import create_engine, engine
#初始化数据库连接,使用pymysql模块
#Mysql的用户root,密码:mysql,端口33306,数据库xxx
engine = create_engine('mysql+pymysql://root:mysql@localhost:3306/xxx')
# 查询语句,选出表中的所有数据

# print(df.head)
# for i in range(29):
#     sql = '''SELECT * FROM tpcode where IMAGETYPE like '''+str(i+1)+'''.%%' limit 2500; '''
#     df = pd.read_sql_query(sql, engine)
#     print(sql)

# with open('a.csv', 'w', encoding='utf-8') as file:
		
#         pd.DataFrame(columns=['REGNO','IMAGETYPE']).to_csv(file, index=False) # 去除index

#         for i in range(29):
#             sql = "SELECT REGNO,IMAGETYPE FROM tpcode where IMAGETYPE like '"+str(i+1)+'.'+"%%"+"' limit 2500; "
#             print(sql)
#             df = pd.read_sql_query(sql, engine)
#             df['IMAGETYPE'] = i+1
#             df.to_csv(file, mode='a+', index=False,header=False)  #追加,不写索引和表头
#             print(i)

from datetime import datetime
import hashlib
from urllib.parse import unquote, quote


import requests

df = pd.read_csv('a.csv')
a = 0
for i in df['REGNO']:
    a = a +1
    imgurl = sign_md5s('BrandInfoFind/GetBrandInfoImg', {"RegId": i})
    try:
        filename = 'image/' +str(i) + '.jpg'
        pic = requests.get(imgurl)
        if pic.status_code == 200:
            with open(filename, 'wb') as fp:
                fp.write(pic.content)
                fp.close()
        if a % 2500 == 0:
            print("成功输出类别:",int(a/2500)+1)
    except:
        print('获取图片失败',i)


#[5356570、221790、688751、8168627、4505830、8009223]
posted @ 2021-08-03 18:31  沐沐子枫  阅读(108)  评论(0编辑  收藏  举报