下载图片-使用正则表达式

前端之家收集整理的这篇文章主要介绍了下载图片-使用正则表达式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

图片处理


如何处理图片

  1. 拿到网页
  2. 使用正则表达式匹配
  3. 使用urlretrieve下载图片
  1. import re
  2. import urllib2
  3. import urllib
  4.  
  5. def getContext(url):
  6. ''' 获取html '''
  7. html = urllib2.urlopen(url)
  8.  
  9. return html.read()
  10.  
  11. def getPicture(html):
  12. ''' 获取图片网址 '''
  13. regex = r'class="BDE_Image" src="(.+?\.jpg)"'
  14.  
  15. pattern = re.compile(regex)
  16.  
  17. imageUrl = re.findall(pattern,html)
  18.  
  19. i = 1
  20. for url in imageUrl:
  21.  
  22. print url
  23.  
  24. urllib.urlretrieve(url,'%s.jpg' % i)
  25.  
  26. i +=1
  27.  
  28. url = 'http://tieba.baidu.com/p/3932177087'
  29. if __name__=='__main__':
  30. html = getContext(url)
  31.  
  32. getPicture(html)

猜你在找的正则表达式相关文章