Python爬虫获取知乎图片

前段时间想抓点知乎问题中的图片,了解了下爬虫,发现还是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'<noscript><img src="(.+?\.jpg)"'
    reg = r'data-actualsrc="(.+?\.jpg)"'
    imgre = re.compile(reg)
    
    imglist = re.findall(imgre,html)
    
  

    x = 0
    for imgurl in imglist:
        urllib.urlretrieve(imgurl,'%s.jpg' % x)
        x+=1


html = getHtml("https://www.zhihu.com/question/24278285")
print getImg(html)

把getHtml中的URL换成自己想获取的知乎问题就可以用了,默认是保存在xxx.py所在的文件夹中.

posted @ 2016-12-09 22:15  洛洛爱吃肉  阅读(412)  评论(0编辑  收藏  举报