一个简单的爬虫练习
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))
本文介绍了一个使用Python进行的简单爬虫实践,展示了如何从百度贴吧抓取图片。通过使用urllib.request和re模块,该爬虫能够获取网页内容并解析出图片链接,最后下载图片到本地。
153

被折叠的 条评论
为什么被折叠?



