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:

import re
import urllib
# Access to web information 
def getHtml(url):
  page = urllib.urlopen(url)
  html =
  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)
html = getHtml("")
print getImg(html)

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

JavaScript regular expression online testing tool:

Online regular expression generation tool:

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: