Share a Python-implemented Bing picture downloader. Download the homepage image and save it to the current directory. The regular library re and the request library are used. The approximate process is as follows:
1, Request fetch first page data 2, re regular match home picture URL3, again use request download picture data
Source:#--*--encoding:utf-8--*--
"""
bingloader.py
Download Bing.com Home Image
"""
ImportRe
ImportSys
ImportOs
ImportRequests
#Parse Get Bing home page
URL ='http://cn.bing.com/'
Print("Request bing.com")
Bingweb = Requests.get (Url=url)
f = open ('test.html','W')
F.write (Bingweb.text)
F.close ()
#Search for picture keywords
Pattern = R'g_img={url:\ ' (http.*jpg) \ ', id:\ ' bgdiv\ ','
m = Re.search (pattern, Bingweb.text)
ifM:
Picurl = M.group (1)
Print("Picture url:\n{0}". Format (Picurl))
Else:
Print("Not a Found picture URL.")
Sys.exit (-1)
filename = Os.path.basename (picurl)
Print('File name:%s'% filename)
ifOs.path.isfile (filename):
Print("The picture [%s] ' has download."% filename)
Raw_input ("Press any key.")
Sys.exit (0)
#Download Image Data
Print("Download Picture ...")
data = Requests.get (picurl,stream=true)
with open (filename,'WB') as Picfile:
forChunkinchData.iter_content (chunk_size=1024):
ifChunk#filter out keep-alive new chunks
Picfile.write (Chunk)
Picfile.flush ()
Picfile.close ()
Print("finished.")Raw_input ("Press any key.")
Bing Picture Downloader (Python implementation)