Bing image download tool (Python implementation ),
Share a Python-implemented Bing image download tool. Download the Home Page image and save it to the current directory. The RegEx and Request libraries are used. The general process is as follows:
1. Request to capture the home page data 2. re regular match the Home Page image URL3, and use Request to download the image data again
Source code: # -- * -- encoding: UTF-8 --*--
"""
Bingloader. py
Download the Bing.com homepage Image
"""
Import re
Import sys
Import OS
Import requests
# Retrieve Bing homepage through resolution
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 image keywords
Pattern = r 'G _ img = {url: \ '(http. * jpg) \', id: \ 'bgdiv \','
M = re. search (pattern, bingweb. text)
If m:
Picurl = m. group (1)
Print ("Picture url: \ n {0}". format (picurl ))
Else:
Print ("Not Found picture url .")
Sys. exit (-1)
Filename = OS. path. basename (picurl)
Print ('file name: % s' % filename)
If OS. 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:
For chunk in data. iter_content (chunk_size = 1024 ):
If chunk: # filter out keep-alive new chunks
Picfile. write (chunk)
Picfile. flush ()
Picfile. close ()
Print ("Finished.") raw_input ("Press any key .")