#coding:utf-8
from urllib import request
import re
def get_html(url):
page = request.urlopen(url)#打开网页
html = page.read() #读取页面源码
htmlcode=html.decode() #在控制台输出ß
print(htmlcode)
return htmlcode
#print(get_html(url1))
def get_img(htmlcode):
reg = r'src="(.+?\.jpg)" width'#正则表达式
reg_img = re.compile(reg)#编译一下,运行更快
imglist = reg_img.findall(htmlcode)#进行匹配
x=0
for img in imglist:
print (img)
request.urlretrieve(img,'/Users/suyue/%s.jpg' % x)
x+=1
print (u'-------网页图片抓取-------')
print (u'请输入url:')
url = input()
if url:
pass
else:
print (u'---没有地址输入正在使用默认地址---')
url = 'http://tieba.baidu.com/p/1753935195'
print (u'----------正在获取网页---------')
html_code = get_html(url)
print (u'----------正在下载图片---------')
get_img(html_code)
print (u'-----------下载成功-----------')
|
|