python 下载网页图片

#下载url中的图片。关键在于re模块的匹配和对urllib库中函数的使用,没什么难度

import urllib
import re

html=None
def get_html(_url):
if _url.startswith('http') or _url.startswith('HTTP'):
url=_url
else :
url='http://'+_url
page=urllib.urlopen(url)
html=page.read()
return html

def get_urls(_html):
url_reg='http:.{10,200}.jpg'
url_list=re.findall(url_reg,_html)
return url_list


def downloadjpg(url_list):
num=0
jpgdir='C:\\Users\\Administrator\\Desktop\\jpg\\'
for i in url_list:
urllib.urlretrieve(i,jpgdir+str(num)+'.jpg')
num+=1

url="http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece763105392230e54f733679f8a4e3d8fc25f93130a1c187be3bb76654c5d949620255af31c04b1ac632d605870ecd392d357ddbe952c2a9f27432a5ad91f069644ef9d49&p=882a971382d257fc57ee913b1554&newp=816cd215d9c152f90be2977b555792694f07d62451d5c343&user=baidu"


html=get_html(url)
urls=get_urls(html)
for i in urls:
print i

downloadjpg(urls)

posted @ 2013-03-21 15:23  Come~on!  阅读(354)  评论(0编辑  收藏  举报