这篇文章主要介绍了python使用正则表达式抓取网页图片的方法,结合具体实例形式分析了python网页文件的读取及正则匹配相关操作技巧,需要的朋友可以参考下
本文实例讲述了Python使用正则表达式抓取网页图片的方法。分享给大家供大家参考,具体如下:
#!/usr/bin/python
import re
import urllib
#获取网页信息
def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html
def getImg(html):
#匹配网页中的图片
reg = r'src="(.*?\.jpg)" alt'
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("http://photo.bitauto.com/?WT.mc_id=360tpdq")
print getImg(html)以上就是使用正则表达式抓取网页图片的Python代码实例的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号