Python爬虫实战(三):简单爬取网页图片
2017-09-04 13:00
881 查看
先上代码:
这样短短的几行代码就把该网页上的图片全部下载下来了。怎样,是不是超级简单。
准备工作:
下载利器
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" 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!")
相关文章推荐
- Python爬虫实战(十一):两种简单的方法爬取动态网页
- Python简单爬虫,爬取网页图片
- python爬虫实战(1)抓取网页图片自动保存
- Python3简单爬虫抓取网页图片
- python3爬虫爬取网页图片简单示例
- Python爬虫学习笔记一:简单网页图片抓取
- [置顶] python3爬虫爬取网页图片简单示例
- Python3网络爬虫:Scrapy入门实战之爬取动态网页图片
- python爬虫实战之最简单的网页爬虫教程
- Python3简单爬虫抓取网页图片
- java爬虫实战简单用Jsoup框架进行网页爬虫(如抓取网页图片)
- python初级实战系列教程《一、爬虫之爬取网页、图片、音视频》
- Python3简单爬虫抓取网页图片
- 【Python3.6爬虫学习记录】(一)爬取简单的静态网页图片
- python爬虫实战 爬取天极图片
- Python爬虫实例(5)-简单的爬虫,用XML爬取卡车图片
- Python3 爬虫(一)-- 简单网页抓取
- Python3 爬虫实战(二)——图片爬虫
- Python爬虫实现抓取网页图片
- Python爬虫---爬取静态网页的图片