Python爬虫获取知乎图片

前段时间想抓点知乎问题中的图片,了解了下爬虫,发现还是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 洛洛爱吃肉 阅读(...) 评论(...) 编辑 收藏

相关文章:

  • 2021-12-05
  • 2022-12-23
  • 2022-12-23
  • 2021-12-10
  • 2021-11-19
  • 2021-07-31
  • 2022-01-09
  • 2021-09-12
猜你喜欢
  • 2021-08-31
  • 2022-02-08
  • 2021-08-24
  • 2021-09-07
  • 2021-07-02
  • 2022-02-04
相关资源
相似解决方案