现在有一批图片要从网上批量存到本地的一个目录,图片的地址是类似这样的:
http://xxxxx.com/web/_photo/pic/200500000001.JPG
http://xxxxx.com/web/_photo/pic/200500000002.JPG
...
http://xxxxx.com/web/_photo/pic/201100000950.JPG
备注:
1.http://xxxxx.com/web/_photo/pic/这一级目录没有权限访问,只能访问单个图片地址。
2.图片的后四位最多到2000,前面的年份从2005一直到2011,中间都是0000。
3.组合起来后有的图片可能会无法访问(可能需要一个if...else: pass之类的语句?)。
在网上找了一个类似的脚本,但是不知道怎么改,这个脚本访问图片的路径都是这样的:
http://xxxxx.com/web/_photo/pic/1.JPG
http://xxxxx.com/web/_photo/pic/2.JPG
http://xxxxx.com/web/_photo/pic/3.JPG
把代码中的 start 和 end 改为 200500000001 和 201100002000 后就提示:
OverflowError: Python int too large to convert to C long
——————————下面是python代码————————————
#!/usr/bin/env python
# coding: utf-8
import os
import time
import urlparse
import string
'''
用于下载资源的小程序
给定样板url设置起止循环点,利用wget批量下载文件。非常适合下载静态图片文件
某些网站经过特殊处理(如登录验证/请求拦截)无法下载
'''
#样板URL,生成的URL列表以此递增
sampleurl='http://xxxxx.com/web/_photo/pic/200500000001.JPG'
#保存图片的文件夹(末尾必须带上斜线),程序会在这个目录下新建一个网站目录
folder='/home/www/test/'
#基准URL循环起点
start=1
#基准URL循环终点
end=30
#在folder下新建当前网站目录
url=urlparse.urlparse(sampleurl)
base=folder+url.hostname+'/'
#解析文件格式
frags=sampleurl.split('/')
ext=frags[len(frags)-1].split('.')[1]
baseurl='/'.join(frags[:-1])
if not os.path.isdir(folder):
os.mkdir(folder)
if not os.path.isdir(base):
os.mkdir(base)
os.chdir(base)
#根据设置的URL的起点与终点生成批量URL地址,保存到文本文件中。
now=time.localtime()
filelist='urls-{0}{1}{2}{3}{4}.txt'.format(now[1],now[2],now[3],now[4],now[5])
f=open(base+filelist,'w')
for x in xrange(start,end,1):
f.write('{0}/{1}.{2}\n'.format(baseurl,x,ext))
f.close()
os.system('wget -i '+filelist)
#如果不希望保存URL列表则删除文件
#os.remove(base+filelist)
http://xxxxx.com/web/_photo/pic/200500000001.JPG
http://xxxxx.com/web/_photo/pic/200500000002.JPG
...
http://xxxxx.com/web/_photo/pic/201100000950.JPG
备注:
1.http://xxxxx.com/web/_photo/pic/这一级目录没有权限访问,只能访问单个图片地址。
2.图片的后四位最多到2000,前面的年份从2005一直到2011,中间都是0000。
3.组合起来后有的图片可能会无法访问(可能需要一个if...else: pass之类的语句?)。
在网上找了一个类似的脚本,但是不知道怎么改,这个脚本访问图片的路径都是这样的:
http://xxxxx.com/web/_photo/pic/1.JPG
http://xxxxx.com/web/_photo/pic/2.JPG
http://xxxxx.com/web/_photo/pic/3.JPG
把代码中的 start 和 end 改为 200500000001 和 201100002000 后就提示:
OverflowError: Python int too large to convert to C long
——————————下面是python代码————————————
#!/usr/bin/env python
# coding: utf-8
import os
import time
import urlparse
import string
'''
用于下载资源的小程序
给定样板url设置起止循环点,利用wget批量下载文件。非常适合下载静态图片文件
某些网站经过特殊处理(如登录验证/请求拦截)无法下载
'''
#样板URL,生成的URL列表以此递增
sampleurl='http://xxxxx.com/web/_photo/pic/200500000001.JPG'
#保存图片的文件夹(末尾必须带上斜线),程序会在这个目录下新建一个网站目录
folder='/home/www/test/'
#基准URL循环起点
start=1
#基准URL循环终点
end=30
#在folder下新建当前网站目录
url=urlparse.urlparse(sampleurl)
base=folder+url.hostname+'/'
#解析文件格式
frags=sampleurl.split('/')
ext=frags[len(frags)-1].split('.')[1]
baseurl='/'.join(frags[:-1])
if not os.path.isdir(folder):
os.mkdir(folder)
if not os.path.isdir(base):
os.mkdir(base)
os.chdir(base)
#根据设置的URL的起点与终点生成批量URL地址,保存到文本文件中。
now=time.localtime()
filelist='urls-{0}{1}{2}{3}{4}.txt'.format(now[1],now[2],now[3],now[4],now[5])
f=open(base+filelist,'w')
for x in xrange(start,end,1):
f.write('{0}/{1}.{2}\n'.format(baseurl,x,ext))
f.close()
os.system('wget -i '+filelist)
#如果不希望保存URL列表则删除文件
#os.remove(base+filelist)