提取数据
#链接数据库,查询数据,每次取5条
sql="select url from bt limit 15, 5;"
下载
def download(filename,img):
try:
pic = requests.get(img, timeout=(3, 7))
filename = './' + new_flie + '/{}'.format(filename)
with open(filename, 'wb') as f:
f.write(pic.content)
f.close()
except:
print('error','URL打不开',img)
完整代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-11-12 15:37
# @Author : Xinru
import pymysql
import requests
import threading
import time
headers = {
"User-Agent": ""
}
#获取url数据
def get_mysql(sql):
db = pymysql.connect(host='127.0.0.1', port=3306, db='movies', user='root', passwd='XXXXX', charset='utf8')
try:
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
cursor.execute(sql)
data = cursor.fetchall()
return data
# print(data[0])
except:
# print('发生错误')
return '发生错误'
finally:
cursor.close()
db.close()
#下载
def download(filename, img):
try:
pic = requests.get(img, headers=headers, timeout=(3, 7))
with open(filename, 'wb') as f:
f.write(pic.content)
f.close()
time.sleep(0.1)
except:
try:
pic = requests.get(img, headers=headers, timeout=(3, 7))
with open(filename, 'wb') as f:
f.write(pic.content)
f.close()
time.sleep(0.1)
except:
print('error', 'URL打不开', img)
def main(sql):
pic_urls = get_mysql(sql)
for ii, pic_url in zip(range(len(pic_urls)), pic_urls):
# for pic_url in pic_urls:
img = pic_url[0][:-6]
pic_name = img.split('/')[-1]
filename = './logo/{}'.format(pic_name)
t = threading.Thread(target=download, args=(filename, img,))#多线程
t.start()
t.join()
print('完成', start)
if __name__ == '__main__':
for i in range(1, 473):
start = 40 * i
sql = "select logo from bt2 limit %d,%d;" % (start, 40)
main(sql)