python 获取网络图片并下载到本地(由网络源码改编)

#coding=utf-8
import re,time
import urllib,urllib2
import os

def getImg(url):
    path_name = url[-9:-5]
    pathl = open_path(path_name)

    html_page = urllib.urlopen(url).read()
    time.sleep(2)
    imgre = re.compile('http://\\S+\/uploads\/\\S+[0-9][0-9]{1,2}\.jpg')
    imglist = imgre.findall(html_page)
    # 去重
    news_imglist = list(set(imglist))
    time.sleep(3)
    x = 1
    for imgurl in news_imglist:
        j = str(x)
        local = pathl+j + '.jpg'
        print  path_name+'文件夹---第'+str(x)+'张'
        urllib.urlretrieve(imgurl, local)
        time.sleep(1)
        x += 1
def get_href(page):
    html = urllib2.urlopen(page)
    hrefs = re.compile('http://\\S+/a/\\d+\.html')
    html_page = html.read()
    links = hrefs.findall(html_page)
    # 去重
    news_links = list(set(links))
    return news_links
def open_path(path_name):
    path = "e:/img/"
    new_path = os.path.join(path, path_name)
    if not os.path.isdir(new_path):
        os.makedirs(new_path)
    new_path1 =new_path+"/"
    return new_path1
def main():
    for i in xrange(1,2):
        htmls = 'http://meizitu.com/a/list_1_%d.html'%i
        time.sleep(2)
        a_hrefs = get_href(htmls)
        for a_href in a_hrefs:
            getImg(a_href)
if __name__ == '__main__':
    main()

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值