Python is an example of a method that USES regular expressions to grab web images

  • 2020-05-30 20:28:20
  • OfStack

This article illustrates how Python USES regular expressions to grab web images. I will share it with you for your reference as follows:


#!/usr/bin/python
import re
import urllib
# Access to web information 
def getHtml(url):
  page = urllib.urlopen(url)
  html = page.read()
  return html
def getImg(html):
# Match the image in the page 
 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)

PS: here are two more handy regular expression tools for you to use:

JavaScript regular expression online testing tool:
http://tools.ofstack.com/regex/javascript

Online regular expression generation tool:
http://tools.ofstack.com/regex/create_reg

More about Python related content to view this site project: the Python regular expression usage summary ", "Python data structure and algorithm tutorial", "Python Socket programming skills summary", "Python function using techniques", "Python string skills summary", "Python introduction and advanced tutorial" and "Python file and directory skills summary"

I hope this article is helpful to you Python programming.


Related articles: