python 批量下載圖片

# -*- coding: cp936 -*-
import urllib2
import imghdr
import os
#connect to a URL
f1 = open("picfiles.txt")             
line = f1.readline()             
n=1
while line:
    n=n+1
    line=line.strip('\n')
    url= str(line)
    url2 = url.replace(" ", "%20") #處理文件包含的空格
    imgname=url.rsplit('/', 1)[-1]
    if os.path.exists('F:\\xyz\\pic\\{}'.format(imgname)):
        print str(n)+' ok\n'
    else:
        content = urllib2.urlopen(url2).read()
        imgtype = imghdr.what('', h=content)
        with open('F:\\xyz\\pic\\{}'.format(imgname), 'wb') as f:
            f.write(content)
            print str(n)+'NO. got Picture name: '+imgname+'\n'
    line = f1.readline()
a=raw_input()
f1.close()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章