# -*- coding: utf-8 -*-
import os
import requests
import pymysql
import _thread
#import threading
# 自定义下载目录
path = 'F:/Reptilian/ysj/'# 线程锁
#lock = threading.Lock()
# 线程树
threadNum = 50# 请求头定义
headers = {
'Connection': 'keep-alive',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Accept':'text/html,application/xhtml+xml,application/xml;\
q=0.9,image/webp,image/apng,*/*;q=0.8',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 \
(KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
}# 打开数据库连接
db = pymysql.connect(
host = '127.0.0.1',
port = 3316,
user = 'lutong',
passwd = 'xxxxx',
db = 'game-video-central',
charset = 'utf8'
)# 连接数据库,并触发下发动作
def getImgUrlFromDB():
# 使用cursor()方法获取操作游标
cursor = db.cursor()
# SQL 查询语句
sql = "select poster_hd from t_series \
where status = 'online'"try:
# 执行SQL语句
cursor.execute(sql)
# 获取所有记录列表
results = cursor.fetchall()
for row in results:
img = row[0]
imgUrl = 'https://mgr-vision.vas.lutongnet.com/game-central-admin/' + img
# 打印结果
#print ("imgUrl=%s" % imgUrl)
download(imgUrl, img)
except:
print ("数据库查询错误!")# 关闭数据库连接
db.close()# 下载操作
def download(url, img):
try:
r = requests.get(url)
print (path + img)
with open(path + img, 'wb') as f:
f.write(r.content)
except requests.exceptions.ConnectionError:
print('资源请求错误!')
return
f.close()# 多线程下载
def loop(url):
while True:
try:
download(url)
except:
print ('线程异常\t%s' % url)
# 创建制定目录
def createDir(path):
path = path.strip()
path = path.rstrip('\\')
isExist = os.path.exists(path)
if not isExist:
os.makedirs(path)
else:
print('目录已存在,不需要重复创建!')
# Main方法
if __name__ == '__main__':
# 创建目录
createDir(path);
getImgUrlFromDB()
#for i in range(0, threadNum):
# t = threading.Thread(target=loop, name='进程号: %s' % i, args = (imgs, ))
# t.start()
Python连接数据库并查询所需数据的实践
最新推荐文章于 2024-08-13 08:00:00 发布