Python captures bing homepage background images

Source: Internet
Author: User

/**
Author: insun
Title: python captures bing homepage background images
Blog: http://yxmhero1989.blog.163.com/blog/static/112157956201311743439712/
**/
The search giant has never seen bing. Recently, I saw an article saying that bing's background image is not bad.
It's true that I want to use python to practice it.
Check the source code with g_img = {url: the following url is the image address. Click the previous page in the lower-right corner to change the image.
If the FireBug in FF does not find the specific path, then HttpFox should catch a package.
 
A json string is loaded into a jpeg file and related information.

http://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&nc=1361089515117&FORM=HYLH1

 

Return json format:
 
{"Images": [{"startdate": "20130216", "fullstartdate": "201302161600", "enddate": "20130217", "url": "http://s.cn.bing.net/az/hprichbg/rb/LongJi_ZH-CN8658435963_1366x768.jpg ", "urlbase": "/az/hprichbg/rb/LongJi_ZH-CN8658435963", "copyright": "Guilin Longji Terrace (? Yoshinori Kuwahara/Flickr/Getty Images) "," copyrightlink ":" http://cn.bing.com/search? Q = % E9 % BE % 99% E8 % 84% 8A % E6 % A2 % AF % E7 % 94% B0 & go = & qs = bs & form = hpcapt "," wp ": false, "hsh": "e688c3f17a0b57306642188adcbf2187", "drk": 1, "top": 1, "bot": 1, "hs": [{"desc ": "In fairy tales, lettuce girls can put down long hair as ropes let the prince climb into the castle,", "link": "http://cn.bing.com/search? Q = % E9 % BB % 84% E6 % B4 % 9B % E7 % 91% B6 % E5 % AF % A8 + % E9 % 95% BF % E5 % 8F % 91% E6 % 9D % 91 & go = & qs = bs & form = hphot1 ", "query": "You can see" lettuce girl "everywhere in the huangluo yaozhai of Longji terraced area "! "," Locx ": 11," locy ": 41 },{" desc ,", "link": "http://cn.bing.com/images/search? Q = % e9 % be % 99% e8 % 84% 8a % e6 % a2 % af % e7 % 94% b0 & FORM = hphot2 "," query ":" Now, it has become a charming curve world-Longji Terrace. "," Locx ": 46," locy ": 49 },{" desc ":" stacked, colorful, grand scale, magnificent, "," link ":" http://cn.bing.com/search? Q = % E6 % 9E % 81% E7 % BE % 8E % E4 % BB % 99% E5 % A2 % 83 + % E4 % B8 % AD % E5 % 9B % BD % E4 % b8 % 83% E5 % A4 % A7 % E6 % A2 % AF % E7 % 94% B0 & go = & qs = bs & form = hphot3 ", "query": "There are more than seven terraces in China. "," Locx ": 60," locy ": 42 },{" desc ":" "Seven Stars and months" is the essence of Longji terraces, composed of a moon farm and seven big mountain bag, about the reason for its formation, "," link ":" http://cn.bing.com/search? Q = % E9 % BE % 99% E8 % 84% 8A % E6 % A2 % AF % E7 % 94% B0 + % E4 % B8 % 83% E6 % 98% 9F % E4 % BC % B4 % E6 % 9C % 88% E7 % 9A % 84% E4 % BC % A0 % E8 % AF % B4 & go = & qs = bs & form = hphot4 ", "query": "There is a beautiful love story ...... "," Locx ": 77," locy ": 40}]," msg ": [{" title ":" Today's picture story "," link ":" http://cn.bing.com/search? Q = % E9 % BE % 99% E8 % 84% 8A % E6 % A2 % AF % E7 % 94% B0 & go = & qs = bs & form = pgbar1 "," text ": "Vibrant Longji terraces" },{ "title": "see pictures, learn English", "link": "http://cn.bing.com/dict/search? Q = % E6 % A2 % AF % E7 % 94% B0 & go = & qs = n & form = pgbar2 "," text ": "Terrace in English"}], "tooltips": {"loading": "loading... "," previous ":" previous Page "," next ":" next page "," walle ":" This image cannot be downloaded as a wallpaper. "," Wils ":" download this image. Connect to Facebook to maximize Bing's functionality. Images can only be used as wallpapers. "}}


Originally I wrote a python capture http://cn.bing.com/the Page code can only capture the picture of the day
#! /Usr/bin/env python #-*-coding: UTF-8-*-#-*-author: insun-*-# python capture bing homepage background image import urllib, re, sysdef get_bing_backphoto (): url = 'HTTP: // cn.bing.com/'html = urllib. urlopen (url ). read () if not html: print 'open & read bing error! 'Return-1 reg = re. compile ("; g_img = {url :'(.*?) '", Re. s) text = re. findall (reg, html) # http://s.cn.bing.net/az/hprichbg/rb/LongJi_ZH-CN8658435963_1366x768.jpg for imgurl in text: right = imgurl. rindex ('/') savepath = imgurl. replace (imgurl [: right + 1], '') urllib. urlretrieve (imgurl, savepath) get_bing_backphoto ()

 

Above can also refer to: http://www.isayme.org/python-get-bing-day-pic.html
Now the idea has changed. You can capture the ajax connection. The parameter n on the previous image link can only be 1 Based on the idx 0-n number. If it is passed to another one, it will always return the data of today. everyone in the program knows this.
You don't need to use python json for processing after reading it, because it is already 'str' type. If you don't believe it, check it.
Then the code is like this. You can also capture the time and add the image to record the day of the image.
#! /Usr/bin/env python #-*-coding: UTF-8-*-#-*-author: insun-*-# python captures all background images on the bing homepage import urllib, re, sys, osdef get_bing_backphoto (): if (OS. path. exists ('photos') = False): OS. mkdir ('photos') for I in range (0,1000): url = 'HTTP: // cn.bing.com/HPImageArchive.aspx? Format = js & idx = '+ str (I) +' & n = 1 & nc = 1361089515117 & FORM = HYLH1 'html = urllib. urlopen (url ). read () if html = 'null': print 'open & read bing error! 'Sys. exit (-1) reg = re. compile ('"url ":"(.*?) "," Urlbase "', re. s) text = re. findall (reg, html) # http://s.cn.bing.net/az/hprichbg/rb/LongJi_ZH-CN8658435963_1366x768.jpg for imgurl in text: right = imgurl. rindex ('/') name = imgurl. replace (imgurl [: right + 1], '') savepath = 'photos/'+ name urllib. urlretrieve (imgurl, savepath) print name + 'Save success! 'Get _ bing_backphoto ()

 



Later I found that when idx is 21, the json data is null. I set a 1000 I, which is really worrying.

Related Article

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.