一个简单的爬虫练习
import urllib.request import re def getHtml(url): page = urllib.request.urlopen(url) #read()返回的结果是二进制的需要转换成字符串 html =page.read().decode("utf-8") return html def getImage(page): reg=r‘src="(.+?\.jpg)" size‘ imgre=re.compile(reg) print (imgre) imglist=re.findall(imgre,page) print (imglist) x=0 for i in imglist: urllib.request.urlretrieve(i,‘%s.jpg‘ %x) x+=1 return imglist html=getHtml("http://tieba.baidu.com/p/4721099001") print(getImage(html))
原文:http://www.cnblogs.com/suye/p/6281615.html