Bing image download tool (Python implementation ),

Source: Internet
Author: User

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 .")

 





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.