Bing Picture Downloader (Python implementation)

Source: Internet
Author: User

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)

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.