下面的代码需要掌握基础的Python知识即可理解,very easy !
代码引用
# coding = utf-8
import urllib
import re
def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html
def getImg(html):
reg = r'src="(.+?)" alt' """正则表达式 过滤图片链接"""
imgre = re.compile(reg)
imglist = re.findall(imgre, html)
print imglist
x = 0
for imgurl in imglist:
imgurl = 'https:'+imgurl """拼接完整URL"""
print imgurl
urllib.urlretrieve(imgurl, '/Users/.../Desktop/img/%s' %x)
x = x+1
html = getHtml("http://www.jianshu.com/")
# print html
print getImg(html)