
前段时间想抓点知乎问题中的图片,了解了下爬虫,发现还是Python的简单方便,于是做了点尝试.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
import urllib import re def (url): page = urllib.urlopen(url) html = page.read() return html def getImg(html): 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所在的文件夹中.
近期评论