Python USES regular expressions to analyze and replace images in web pages

  • 2020-04-02 14:42:34
  • OfStack

This example shows how python USES regular expressions to analyze and replace images in web pages. Share with you for your reference. Specific analysis is as follows:

This code analyzes all the picture forms in the web page < img > After analysis, add corresponding decoration tags before and after it, and add to the hyperlink of the picture.

result = value.replace("[ page ]","").replace('  ',u' ')
p=re.compile(r'''(<imgb[^<>]*?bsrc[strn]*=[strn]*["']?[strn]*([^strn"'<>]*)[^<>]*?/?[strn]*>)''',re.IGNORECASE)
result = p.sub(r'''<span class="openIcon"><em></em><a href="2">1</a></span>''',result)

PS: here for you to provide two very convenient regular expression tools for your reference:

JavaScript regular expression online testing tool:
(link: http://tools.jb51.net/regex/javascript)

Regular expression online generation tool:
(link: http://tools.jb51.net/regex/create_reg)

I hope this article has helped you with your Python programming.


Related articles: