抓取网页资源并且下载

#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="(.+?\.jpg)" pic_ext'

reg = r'src="(.+?\.jpg)"'

imgre = re.compile(reg)

imglist = re.findall(imgre,html)

# return imglist

x = 0

for imgurl in imglist:

urllib.urlretrieve(imgurl,'D:\WWW\demo\python\curl\\img5\\%s.jpg' % x)

x+=1

return imglist

a=10

b=list(range(3))

for y in b:

url = "https://www.zhihu.com/topic/19552207/top-answers?page="

html = '%s%s'%(url,a)

html = getHtml(html)

print getImg(html)

# res =  getImg(html)

# print res

a+=1

你可能感兴趣的:(抓取网页资源并且下载)