先上代码:
#coding=utf-8
import urllib.request
for i in range(1,41):
imgurl = "http://mtl.ttsqgs.com/images/img/11552/"
imgurl += str(i) + ".jpg"
urllib.request.urlretrieve(imgurl,'%s.jpg' % i)
这样短短的几行代码就把该网页上的图片全部下载下来了。怎样,是不是超级简单。
准备工作:
下载利器
urllib.request.urlretrieve
查看帮助
help(urllib.request.urlretrieve)
Help on function urlretrieve in module urllib.request:
urlretrieve(url, filename=None, reporthook=None, data=None)
Retrieve a URL into a temporary location on disk.
Requires a URL argument. If a filename is passed, it is used as
the temporary file location. The reporthook argument should be
a callable that accepts a block number, a read size, and the
total file size of the URL target. The data argument should be
valid URL encoded data.
If a filename is passed and the URL points to a local resource,
the result is a copy from local file to new file.
Returns a tuple containing the path to the newly created
data file as well as the resulting HTTPMessage object.
你只需要一个下载链接并设置好参数,即可下载。
图片分析:
网站: https://www.meitulu.com/item/11552.html
右键单击正文里的图片 —> 选择 查看图像信息。
http://mtl.ttsqgs.com/images/img/11552/1.jpg
再看看地址框类的图片,
http://mtl.ttsqgs.com/images/img/11552/2.jpg
http://mtl.ttsqgs.com/images/img/11552/3.jpg
http://mtl.ttsqgs.com/images/img/11552/4.jpg
这规律也是没谁了。
网站介绍
发行机构: 猫萌榜
期刊编号: Vol.023
图片数量: 40 张
也就是说最后一张的链接:
http://mtl.ttsqgs.com/images/img/11552/40.jpg
写出代码:
for i in range(1,41): #半开半闭区间[1,41)
imgurl = “http://mtl.ttsqgs.com/images/img/11552/”
imgurl += str(i) + “.jpg” #设置好链接,进行循环, str(i)将类型i转换为字符串
urllib.request.urlretrieve(imgurl,’%s.jpg’ % i) #下载到当前目录
当然,一般都需要查看下载进度,可以这样修改:
#coding=utf-8
import urllib.request
for i in range(1,41):
imgurl = "http://mtl.ttsqgs.com/images/img/11552/"
imgurl += str(i) + ".jpg"
print ("正在下载第{}张图片".format(i))
urllib.request.urlretrieve(imgurl,'%s.jpg' % i)
print ("OK!DownLoad ALL!")