# -*- coding: utf-8 -*-
import os
import requests
import pymysql
import _thread
#import threading
# Custom download directory
path = 'F: / Reptilian / ysj /'# Thread lock
#lock = threading.Lock ()
# thread tree
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',
}# Open Database Connectivity
DB = pymysql.connect (
Host = '127.0.0.1',
Port = 3316,
User = 'Lutong',
the passwd = 'XXXXX',
DB = 'Video-Game-Central',
charset = 'UTF8'
)# Connect to the database, and trigger actions issued under
def getImgUrlFromDB ():
Gets the operating cursor # use cursor () method of
the Cursor = db.cursor ()
# SQL query
SQL = "poster_hd from the SELECT t_series \
the WHERE Status = 'Online'"the try:
# execute SQL statements
cursor.execute (SQL)
# get a list of all the records
Results = cursor.fetchall ()
for Row in Results:
img = Row [0]
imgUrl = 'HTTPS: //mgr-vision.vas.lutongnet. COM / Game-ADMIN-Central / '+ IMG
# printing result
#Print ( "% S = for imgUrl" for imgUrl%)
downloads (for imgUrl, IMG)
the except:
Print ( "database query error!")# Close the database connection
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()# Multi-threaded download
DEF Loop (url):
the while True:
the try:
download (url)
the except:
Print ( 'thread exception \ t% S'% url)
# Create a directory developed
DEF createDir (path):
path = path.strip ( )
path = path.rstrip ( '\\')
ISEXIST = os.path.exists (path)
IF not ISEXIST:
os.makdirs (path)
the else:
Print ( '! directory already exists, do not need to be repeated to create a')
# Main method
IF the __name__ == '__main__':
# Create a directory
createDir (path);
getImgUrlFromDB ()
#for I in Range (0, threadNum):
# = T of the threading.Thread (target = Loop, name = 'process ID:% s '% i, args = (imgs ,))
# t.start()
Python connect to the database and query the data needed to practice
Guess you like
Origin blog.csdn.net/lierwang2017/article/details/94717899
Recommended
Ranking