从网页页面上批量下载jpg格式图片,并按照数字递增命名保存到指定的文件夹

Web地址:http://news.weather.com.cn/2017/12/2812347.shtml

打开网页,点击F12查看

 

代码实现:

import urllib
import urllib.request
import re

#解析页面
def load_page(url):
    request=urllib.request.Request(url) #发送网络请求
    response=urllib.request.urlopen(request) #根据url打开页面
    data=response.read() #获取页面响应数据
    return data


#下载图片
def get_image(html):
    regx=r\'http://[\S]*jpg\'  #定义正则表达式
    pattern=re.compile(regx)   #编译表达式构造匹配模式
    get_image=re.findall(pattern,repr(html))  #进行正则匹配并返回结果

    num = 1
    #遍历获取的图片
    for img in get_image:
        image=load_page(img)
        #将图片存入到指定文件夹
        with open(\'E:\\Photo\\%s.jpg\' %num,\'wb\') as fb:
            fb.write(image)
            print("正在下载第%s张图片" %num)
            num = num + 1
    print("下载完成!")

url=\'http://news.weather.com.cn/2017/12/2812347.shtml\'
html=load_page(url)
get_image(html)

 结果:

 

版权声明:本文为NancyRM原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/NancyRM/p/8066200.html