前端之家收集整理的这篇文章主要介绍了
下载图片-使用正则表达式,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
- 拿到网页
- 使用正则表达式匹配
- 使用urlretrieve下载图片
- import re
- import urllib2
- import urllib
-
- def getContext(url):
- ''' 获取html '''
- html = urllib2.urlopen(url)
-
- return html.read()
-
- def getPicture(html):
- ''' 获取图片网址 '''
- regex = r'class="BDE_Image" src="(.+?\.jpg)"'
-
- pattern = re.compile(regex)
-
- imageUrl = re.findall(pattern,html)
-
- i = 1
- for url in imageUrl:
-
- print url
-
- urllib.urlretrieve(url,'%s.jpg' % i)
-
- i +=1
-
- url = 'http://tieba.baidu.com/p/3932177087'
- if __name__=='__main__':
- html = getContext(url)
-
- getPicture(html)