python批量出图_Python 批量下载图片示例

使用Python find函数和urllib下载图片。

A:#!/usr/bin/env python

import time

import urllib

i = 0

url = ['']*10

name = ['']*10

con = urllib.urlopen('http://www.ithome.com/html/bizhi/164396.htm').read()

src = con.find(r'/newsuploadfiles')

end = con.find(r'.jpg',src)

name[0] = con[src +24 :end +1]

while src != -1 and end != -1 and i 

url[0] = con[src -21 :end +4]

src = con.find(r'/newsuploadfiles',end)

end = con.find(r'.jpg',src)

content = urllib.urlopen(url[0]).read()

open(r'img/'+ name[0]+'jpg','w+').write(content)

name[0] = con[src +24 :end +1]

print url[0]

time.sleep(3)

i = i + 1

else:

print "Download Over!"

B:#!/usr/bin/env python

import re

import urllib

def getHtml(url):

page = urllib.urlopen(url)

html = page.read()

return html

def getImg(html):

reg = r'data-original="(.+?\.jpg)" />'

imgre = re.compile(reg)

imglist = re.findall(imgre,html)

i = 0

for imgurl in imglist:

name = imgurl[45:]

print name

content = urllib.urlopen(imgurl).read()

open(r'tmp/' + name,'w+').write(content)

i = i + 1

html = getHtml("http://www.ithome.com/html/bizhi/164396.htm")

print getImg(html)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值