Python's n small functions (connect to the database and download the image in the appropriate location)

Source: Internet
Author: User

########################################################################################################### # # 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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.