# -*- coding: cp936 -*-
import urllib2
import os
import imghdr
#connect to a URL
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/51.0.2704.63 Safari/537.36'}
f1 = open("pic.txt") # 返回一个文件对象
line = f1.readline() # 调用文件的 readline()方法
count=0
while line:
line=line.strip('\n')
url= str(line)
url2=urllib2.quote(url)
url2=url2.replace("http%3A//","http://")
imgname=url.rsplit('/', 1)[-1]
count+=1
fold2='F:\\采集新\\fr\\pic\\'+imgname
if not os.path.exists(fold2):
req = urllib2.Request(url=url2,headers=headers)
content = urllib2.urlopen(req).read()
imgtype = imghdr.what('', h=content)
with open('F:\\采集新\\fr\\pic\\{}'.format(imgname), 'wb') as f:
f.write(content)
print count
line = f1.readline()
a=raw_input()
f1.close()
Python 批量下载文件
最新推荐文章于 2024-03-13 20:43:21 发布