有没有办法让Bing的一天的照片?
有没有办法以编程方式获取Bing背景图片?
必应的API似乎没有提供这样的function,也许有另一种方式?
我认为最好的方法是模仿他们通过AJAX调用自己做的方式。
他们调用这个URL并通过XML反序列化来提取信息。
XML: http : //www.bing.com/HPImageArchive.aspx?format= xml& idx=0&n=1& mkt= en-US
JSON: http : //www.bing.com/HPImageArchive.aspx? format = js&idx=0&n =1& mkt = en-US
RSS: http : //www.bing.com/HPImageArchive.aspx? format = rss& idx=0&n=1& mkt= en-US
mkt属性显然可以更改为与“en-US”不同的区域,但是如果您不关心某个特定区域,也可以将其完全退出。
请注意,添加到图像名称的_1366x768.jpg
后缀似乎可以修改为不同的分辨率(例如全高清和其他可能的_1920x1080.jpg
)。
以下是截至2013年9月28日的XML格式的数据转储。
<?xml version="1.0" encoding="utf-8"?> <images> <image> <startdate>20130928</startdate> <fullstartdate>201309280000</fullstartdate> <enddate>20130929</enddate> <url>/az/hprichbg/rb/LakeTurkana_EN-US15976511099_1366x768.jpg</url> <urlBase>/az/hprichbg/rb/LakeTurkana_EN-US15976511099</urlBase> <copyright>Lava rock pools at the southern end of Lake Turkana, in Kenya (© Nigel Pavitt/Corbis)</copyright> <copyrightlink>http://www.bing.com/search?q=Lake+Turkana%2C+Kenya&form=hpcapt</copyrightlink> <drk>1</drk> <top>1</top> <bot>1</bot> <hotspots> <hotspot> <desc>These are the southern reaches of a lake...</desc> <link>http://www.bing.com/maps/?v=2&cp=2.794725~37.335197&lvl=7&dir=0&sty=b&q=Lake%20Turkana%2C%20Kenya&form=hphot1</link> <query>That stretches for 180 miles, up into another country</query> <LocX>15</LocX> <LocY>33</LocY> </hotspot> <hotspot> <desc>This body of water was once called the Jade Sea.</desc> <link>http://www.bing.com/search?q=green+algae&form=hphot2</link> <query>What gives the water here its sometimes vibrant hue?</query> <LocX>37</LocX> <LocY>42</LocY> </hotspot> <hotspot> <desc>One of the world's most powerful predators lives here.</desc> <link>http://www.bing.com/videos/search?q=Underwater+Croc+Cams+National+Geographic&FORM=hphot3#view=detail&mid=D25E1909D3514A8732C5D25E1909D3514A8732C5</link> <query>See some rare underwater footage of the beast</query> <LocX>66</LocX> <LocY>33</LocY> </hotspot> <hotspot> <desc>Many fossils of ancient human ancestors have been uncovered in the surrounding area.</desc> <link>http://www.bing.com/search?q=Turkana+Boy&form=hphot4</link> <query>One skeleton was so complete, paleoanthropologists gave him a name</query> <LocX>82</LocX> <LocY>41</LocY> </hotspot> </hotspots> <messages></messages> </image> <tooltips> <loadMessage> <message>Indlæser...</message> </loadMessage> <previousImage> <text>Forrige</text> </previousImage> <nextImage> <text>Næste</text> </nextImage> <play> <text>Afspil</text> </play> <pause> <text>Pause</text> </pause> </tooltips> </images>
BING IMAGE的JSON格式
我find了一种获取当天Bing图像的JSON格式的方法
注意
n=
你想要的图像号(你可以使用整数 ),
mkt=
您的位置(例如: en-US )
这里是JSON输出的样子
{ "images": [ { "startdate": "20141214", "fullstartdate": "201412141830", "enddate": "20141215", "url": "\/az\/hprichbg\/rb\/BlackButte_EN-IN7038391888_1920x1080.jpg", "urlbase": "\/az\/hprichbg\/rb\/BlackButte_EN-IN7038391888", "copyright": "Black Butte, seen from the Mount Jefferson Wilderness, Oregon, USA (\u00a9 Marc Adamus\/Aurora Photos)", "copyrightlink": "http:\/\/www.bing.com\/search?q=Black+Butte&qs=n&form=hpcapt&mkt=en-in&pq=black+butte&sc=8-11&sp=-1&sk=&cvid=228ac7f125f94bbaafd4a4abd4f9a32d", "wp": true, "hsh": "94156ae1e2e1be49f9b739d2b7bff65c", "drk": 1, "top": 1, "bot": 1, "hs": [ ], "msg": [ { "title": "How does it feel\u2026", "link": "http:\/\/www.bing.com\/videos\/search?q=Climbing+Black+Butte&FORM=pgbar1&mkt=en-in#view=detail&mid=58BDB2F2B9FCB85D597558BDB2F2B9FCB85D5975", "text": "To climb 1961.7 m?" }, { "title": "On top of the world", "link": "http:\/\/www.bing.com\/images\/search?q=Pictures+From+the+Top+of+Mount+Everest&FORM=pgbar2&mkt=en-in", "text": "It's mountaineer's dream view" } ] } ], "tooltips": { "loading": "Loading...", "previous": "Previous", "next": "Next", "walle": "This image is not available to download as wallpaper.", "walls": "Download this image. Use of this image is restricted to wallpaper only." } }
使用images[]
的url
并将其添加到'http://bing.com'
这里的url是"url": "\/az\/hprichbg\/rb\/DayGecko_EN-US8730336235_1366x768.jpg"
微软最近发布了一个Windows 7的必应dynamic主题 ,其中包含一个链接到Bing壁纸的RSS源 。
还有一个Python脚本会尝试加载Bing网站,并猜测正确的图像URL,但从我的经验来看,它通常会产生比RSS提供的分辨率更低的图像。
我迟到了,但是如果有人需要PHP实现,我写了一个简单的类来处理这个过程:
我也喜欢Bing图像,但是为了下载图像,他们的应用程序太臃肿了。 在分析与提琴手的连接后,我写了这个代码。 1920x1200embedded了Bing标志,但较低的分辨率没有它。
您可以让Windows显示您设置的图像文件夹中的随机图像,以便每天下载图像文件,它会显示更多的随机图像。 如果你保持“imageDir”,你需要改变该文件夹的权限,否则会崩溃,我不打扰捕捉错误。 最后,如果要将墙纸设置为今天的图像,或者您可以创build任务来运行该程序,请在login后说一分钟,然后取消注释行。
using System; using System.IO; using System.Net; //using System.Runtime.InteropServices; namespace Bing { class Program { // [DllImport("user32.dll", CharSet = CharSet.Auto)] // private static extern Int32 SystemParametersInfo(UInt32 uiAction, UInt32 uiParam, String pvParam, UInt32 fWinIni); private static String imgDir = @"C:\Windows\Web\Wallpaper\Bing\"; static void Main(string[] args) { String imageFileName; if (!Directory.Exists(imgDir)) Directory.CreateDirectory(imgDir); for (byte i = 6; i >= 0; i--) { imageFileName = imgDir + DateTime.Today.AddDays(-i).ToString("yyy-MM-dd") + ".jpg"; if (!File.Exists(imageFileName)) { string response = null; Connect(ref response, i); ProcessXml(ref response); using (WebClient client = new WebClient()) client.DownloadFile("http://www.bing.com" + response + "_1920x1200.jpg", imageFileName); } } //SystemParametersInfo(20, 0, imageFileName, 0x01 | 0x02); } private static void Connect(ref string res, byte i) { HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create("http://www.bing.com/hpimagearchive.aspx?format=xml&idx=" + i + "&n=1&mbl=1&mkt=en-ww"); webrequest.KeepAlive = false; webrequest.Method = "GET"; using (HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse()) using (StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream())) res = loResponseStream.ReadToEnd(); } private static void ProcessXml(ref string xmlString) { using (System.Xml.XmlReader reader = System.Xml.XmlReader.Create(new StringReader(xmlString))) { reader.ReadToFollowing("urlBase"); xmlString = reader.ReadElementContentAsString(); } } } }
我看到很多人也在寻求新的RSS链接,只需将URL中的格式参数更改为“rss”即可。
RSS : http : //www.bing.com/HPImageArchive.aspx? format = rss & idx=0&n=1& mkt = en-US
这个JavaScript将通过改变div的背景图像到当天的bing图像的背景来回答“如何处理api”。
function PullBackground() { var ajaxRequest = new XMLHttpRequest(), background = ''; ajaxRequest.open('POST', "http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1&mkt=en-US", true); ajaxRequest.setRequestHeader("Connection", "close"); ajaxRequest.send(''); ajaxRequest.onreadystatechange = function () { if (ajaxRequest.readyState == 4) { background = ajaxRequest.responseText; var res = background.split("<url>"); var res1 = res[1].split("</url>"); background = res1[0]; document.getElementById('NameOfTheDivToChange').style.backgroundImage = "url('http://bing.com" + background + "')" document.getElementById('NameOfTheDivToChange').style.backgroundSize = "100%"; } } }
如果有人正在寻找可能的实现,我用C#编写了一个小命令行程序来下载,保存和设置我的背景为当天的Bing图像。 随意修改它以满足您的个人需求。 https://github.com/josueespinosa/BingBackground
using Microsoft.Win32; using Newtonsoft.Json; using System; using System.Drawing; using System.IO; using System.Net; using System.Runtime.InteropServices; using System.Windows.Forms; namespace BingBackground { class BingBackground { private static void Main(string[] args) { string urlBase = GetBackgroundUrlBase(); Image background = DownloadBackground(urlBase + GetResolutionExtension(urlBase)); SaveBackground(background); SetBackground(background, PicturePosition.Fill); } private static dynamic DownloadJson() { using (WebClient webClient = new WebClient()) { Console.WriteLine("Downloading JSON..."); string jsonString = webClient.DownloadString("https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US"); return JsonConvert.DeserializeObject<dynamic>(jsonString); } } private static string GetBackgroundUrlBase() { dynamic jsonObject = DownloadJson(); return "https://www.bing.com" + jsonObject.images[0].urlbase; } private static string GetBackgroundTitle() { dynamic jsonObject = DownloadJson(); string copyrightText = jsonObject.images[0].copyright; return copyrightText.Substring(0, copyrightText.IndexOf(" (")); } private static bool WebsiteExists(string url) { try { WebRequest request = WebRequest.Create(url); request.Method = "HEAD"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); return response.StatusCode == HttpStatusCode.OK; } catch { return false; } } private static string GetResolutionExtension(string url) { Rectangle resolution = Screen.PrimaryScreen.Bounds; string widthByHeight = resolution.Width + "x" + resolution.Height; string potentialExtension = "_" + widthByHeight + ".jpg"; if (WebsiteExists(url + potentialExtension)) { Console.WriteLine("Background for " + widthByHeight + " found."); return potentialExtension; } else { Console.WriteLine("No background for " + widthByHeight + " was found."); Console.WriteLine("Using 1920x1080 instead."); return "_1920x1080.jpg"; } } private static Image DownloadBackground(string url) { Console.WriteLine("Downloading background..."); WebRequest request = WebRequest.Create(url); WebResponse reponse = request.GetResponse(); Stream stream = reponse.GetResponseStream(); return Image.FromStream(stream); } private static string GetBackgroundImagePath() { string directory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "Bing Backgrounds", DateTime.Now.Year.ToString()); Directory.CreateDirectory(directory); return Path.Combine(directory, DateTime.Now.ToString("Md-yyyy") + ".bmp"); } private static void SaveBackground(Image background) { Console.WriteLine("Saving background..."); background.Save(GetBackgroundImagePath(), System.Drawing.Imaging.ImageFormat.Bmp); } private enum PicturePosition { Tile, Center, Stretch, Fit, Fill } internal sealed class NativeMethods { [DllImport("user32.dll", CharSet = CharSet.Auto)] internal static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni); } private static void SetBackground(Image background, PicturePosition style) { Console.WriteLine("Setting background..."); using (RegistryKey key = Registry.CurrentUser.OpenSubKey(Path.Combine("Control Panel", "Desktop"), true)) { switch (style) { case PicturePosition.Tile: key.SetValue("PicturePosition", "0"); key.SetValue("TileWallpaper", "1"); break; case PicturePosition.Center: key.SetValue("PicturePosition", "0"); key.SetValue("TileWallpaper", "0"); break; case PicturePosition.Stretch: key.SetValue("PicturePosition", "2"); key.SetValue("TileWallpaper", "0"); break; case PicturePosition.Fit: key.SetValue("PicturePosition", "6"); key.SetValue("TileWallpaper", "0"); break; case PicturePosition.Fill: key.SetValue("PicturePosition", "10"); key.SetValue("TileWallpaper", "0"); break; } } const int SetDesktopBackground = 20; const int UpdateIniFile = 1; const int SendWindowsIniChange = 2; NativeMethods.SystemParametersInfo(SetDesktopBackground, 0, GetBackgroundImagePath(), UpdateIniFile | SendWindowsIniChange); } } }
看看Github上的bing-desktop-wallpaper-changer 。 脚本已经用python编写了,我希望你能在那里find答案。
#!/usr/bin/python #-*- coding: utf-8 -*- import os import urllib import urllib2 from bs4 import BeautifulSoup # Get BingXML file which contains the URL of the Bing Photo of the day # idx = Number days previous the present day. 0 means current day, 1 means yesterday, etc # n = Number of images predious the day given by idx # mkt denotes your location. eg en-US means United States. Put in your country code BingXML_URL = "http://www.bing.com/HPImageArchive.aspx? format=xml&idx=0&n=1&mkt=en-US" page = urllib2.urlopen(BingXML_URL) BingXML = BeautifulSoup(page, "lxml") # For extracting complete URL of the image Images = BingXML.find_all('image') ImageURL = "https://www.bing.com" + Images[0].url.text ImageName = Images[0].startdate.text+".jpg" urllib.urlretrieve(ImageURL, ImageName)
看看Github项目的详细代码
一个class轮PowerShell(3.0或更高版本)
irm
是Invoke-RestMethod
的别名
irm "bing.com$((irm "bing.com/HPImageArchive.aspx?format=js&mkt=en-IN&n=1").images[0].url)" -OutFile bing.jpg
您可能会考虑获取该url的file_content并search该图像的文件。 不知道这是最好的方式,但它是一种方式。
我有麻烦得到一个正确的RSS饲料,我可以使用约翰的背景切换,直到我发现这feedburner饲料似乎工作得很好: http : //feeds.feedburner.com/bingimages
使用来自@Siv的URL,下面是更新<div class="bgimg" id="background">
的JavaScript示例
function GetImageURL(ans) { var suffix = ans.images[0].url document.getElementById("background").style.backgroundImage = 'url("' + 'http://bing.com/' + suffix + '"' } function GetJSON() { var xmlhttp = new XMLHttpRequest() var url = "https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1" xmlhttp.onreadystatechange = function () { if (this.readyState == 4 && this.status == 200) { var ans = JSON.parse(this.responseText); GetImageURL(ans); } }; xmlhttp.open("GET", url, true); xmlhttp.send(); } window.onload = function () { GetJSON() }
这个类的CSS是:
.bgimg { height: 100%; position: relative; opacity: 0.95; background-position: center; background-repeat: no-repeat; background-size: cover; }