This article mainly introduces how to capture web images and put them in a specified folder by using python. you can refer to python to capture web images and put them in a specified folder.
The code is as follows:
#-*-Coding = utf-8 -*-
Import urllib2
Import urllib
Import socket
Import OS
Import re
Def Docment ():
Print u'store the file in E: \ Python \ graph (enter a number or letter )'
H = raw_input ()
Path = u'e: \ Python \ Fig + str (h)
If not OS. path. exists (path ):
OS. makedirs (path)
Return path
Def getallurl (html ):
Reg = r "a href = '(.*? \. Htm )'"
Allurl = re. compile (reg)
AllList = re. findall (allurl, html)
Return allList
Def getHTML (url ):
Url = url
Req_header = {'user-Agent': 'mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv: 1.9.1.6) Gecko/20091201 Firefox/3.5.6 '}
Req_timeout = 20
Html = 'cuowu'
Try:
Req = urllib2.Request (url, None, req_header)
Resp = urllib2.urlopen (req, None, req_timeout)
Html = resp. read ()
Failed T urllib2.URLError as e:
Print e. message
Failed T socket. timeout as e:
GetHTML (url, fu)
Return html
Def getImg (html, path ):
Reg = r 'IMG class = IMG_show border = 0 src = (.*? \. Jpg )'
Imgre = re. compile (reg)
ImgList = re. findall (imgre, html)
If imgList:
Print 'ghasghg', path
For imgurl in imgList:
Print imgurl
Content2 = urllib2.urlopen (imgurl). read ()
With open (path + '/' + imgurl [-7:], 'wb') as code:
Code. write (content2)
Else:
Return 0
Def getallurl (html ):
Reg = r "a href = '(.*? \. Htm )'"
Allurl = re. compile (reg)
AllList = re. findall (allurl, html)
Return allList
J = 1
I = 0
Print U' enter the URL :'
Ul = raw_input ()
Print u'start Download'
Print u'quarter '+ str (j) + u' page'
Html = getHTML (ul)
AllList = getallurl (html)
Path = Docment ()
GetImg (html, path)
While I For lis in allList:
L = lis [I]
Url = r 'http: // www. umei. cc/p/gaoqing/rihan/'+ lis
I = I + 1
J = j + 1
Html = getHTML (url)
GetImg (html, path)
Print u'quarter '+ str (j) + u' page'
Else:
Print u'download finished'