Python 爬虫——爬取Web页面图片

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

Web地址:http://p.weather.com.cn/2017/06/2720826.shtml#p=1 

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://p.weather.com.cn/2017/06/2720826.shtml#p=1'

html=load_page(url)

get_image(html)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值