########################################################################################################### # # connection database, mysqldb.py###########################
###############################################################################
#-*-Coding:utf-8-*-
"""
Created on Fri Mar 20 10:50:56 2015
@author: SL
"""
Import OS
Import MySQLdb
Mysql_name = os.getenv (' mysql_name ', ' AAA ')
Mysql_user = os.getenv (' Mysql_user ', ' BBB ')
Mysqlpassword = os.getenv (' Mysql_password ', ' CCC ')
Mysql_host = os.getenv (' mysql_host ', ' ddd ')
Mysql_port = os.getenv (' mysql_port ', ' eee ')
Def connect ():
Db=mysqldb.connect (Host=mysql_host, User=mysql_user, Passwd=mysqlpassword, Db=mysql_name, charset= "UTF8")
Return db
###############################################################################
##################### Loop read Download the appropriate location picture ##################################
###############################################################################
#-*-Coding:utf-8-*-
"""
Created on Mon Mar 06 14:55:15 2017
@author: SL
"""
Import MySQLdb
#import datetime
Import Pandas as PD
db = MySQLdb.connect ()
Sql= ""
SELECT
Aaa
BBB as URLs
From
Ccc
GROUP by
Aaa
‘‘‘
DF = pd.read_sql (sql, CON=DB)
Db.close ()
From PIL import Image
From IO import Bytesio
Import requests
Import OS
From PIL import ImageFile
Imagefile.load_truncated_images = True
def download_jpg (URL, name):
Response = Requests.get (URL). Content ######### #获取url对应内容
img = Image.open (Bytesio (response)) ######### #打开图片
Img.save (name) ######## #保存图片
if __name__ = = ' __main__ ':
Df1=df.set_index (' aaa ') # # # #以第一列为index, second column content
For Mulu in df[' aaa ': # # # #得到第一列全部内容并循环
If not os.path.exists (R ' E:\pp\%s '% (Mulu)):
Os.makedirs (R ' E:\pp\%s '% (Mulu))
For URL in Df1.loc[mulu]: # # # #由第一列内容对应位置得到第二列内容
#print URL
Bb=url.split (', ')
Lens=len (BB)
For I in range (lens):
Download_jpg (' http://aaa=%s '% (str (bb[i])), R ' E:\pp\%s\%s.jpg '% (mulu,str (mulu) +str (i))) ##### #下载对应url区域图片, and folder + Ordinal name
Python's n small functions (connect to the database and download the image in the appropriate location)