python简单爬虫(下载知乎图片示例)

#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="(http.+?\.jpg)"'
    imgre = re.compile(reg)
    imglist = re.findall(imgre,html)
    print imglist
    x = 0
    for imgurl in imglist:
        urllib.urlretrieve(imgurl,'%s.jpg' % x)
        print imgurl
        x+=1

if __name__ == '__main__':
    print 'start...'
    html = getHtml("https://www.zhihu.com/question/30950901")
    # print html
    getImg(html)
    print 'The end.'

你可能感兴趣的:(python简单爬虫(下载知乎图片示例))