This article will share with you my first work. it is very simple and practical to use Python regular expression matching to capture image code. we recommend it to you, and you can freely expand it. Connotation: regular expression matching, regular expression replacement, page capturing, and image saving.
Practical First Python code reference
#! /Usr/bin/env pythonimport urllibimport re x = 0def getHtml (url): page = urllib. urlopen (url) html = page. read () return html def getImg (html): global x reg = 'alt = ". +? "Src =" (. +? \. Jpg) "'imgre = re. compile (reg) imglist = re. findall (imgre, html) for imgurl in imglist: urllib. urlretrieve (re. sub (r', \ d +, \ d + ',', 100', imgurl), "img/%s.jpg" % x) print "\ n" + re. sub (r', \ d +, \ d + ',', 100', imgurl) + "=" + "img/%s.jpg" % x + = 1 print 'starting... 'pages = range () for p in pages: html = getHtml ('http: // m.lovebizhi.com/category/7655/mongod/' % p) print "\ n ----------------------- pag E: % d ------------------------------- "% p getImg (html) print" \ nDone! "
The above is all the code I have shared with you in this article. I am a Python newbie, the first one. I hope it will help you.