nohapa
  • 累计撰写 19 篇文章
  • 累计收到 0 条评论

获取必应每日壁纸(xml和json两种方法)java版

nohapa
2020-10-29 / 0 评论 / 139 阅读 / 正在检测是否收录...
必应壁纸bing

必应搜索界面,每天主页都会更新一张壁纸(有的非常漂亮)。

bing

而且必应也提供api来获取壁纸信息,xml和json两种格式。

xml-API(返回1366x768分辨率)

格式format=xml(返回xml格式),idx=0(0代表今天,1代表昨天,最大为8),n=1(1显示一天信息,最大为8)

http://www.bing.com/HPImageArchive.aspx?format=xml&;idx=0&n=1

  • 返回如下xml格式
<images>
<image>
<startdate>20180628</startdate>
<fullstartdate>201806280900</fullstartdate>
<enddate>20180629</enddate>
<url>
/az/hprichbg/rb/AuroraPhotographer_ZH-CN11480495787_1366x768.jpg
</url>
<urlBase>
/az/hprichbg/rb/AuroraPhotographer_ZH-CN11480495787
</urlBase>
<copyright>在乌塔克雷夫海滩拍摄的北极光,挪威 (© David Clapp/Getty Images)</copyright>
<copyrightlink>
http://www.bing.com/search?q=%E6%9E%81%E5%85%89&;form=hpcapt&mkt=zh-cn
</copyrightlink>
<drk>1</drk>
<top>1</top>
<bot>1</bot>
<hotspots/>
</image>
<tooltips>
<loadMessage>
<message>Loading...</message>
</loadMessage>
<previousImage>
<text>Previous image</text>
</previousImage>
<nextImage>
<text>Next image</text>
</nextImage>
<play>
<text>Play video</text>
</play>
<pause>
<text>Pause video</text>
</pause>
</tooltips>
</images>
  • java代码
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("https://www.bing.com/HPImageArchive.aspx?format=xml&;idx=0&n=1");
document.normalize();
String relativePath = document.getElementsByTagName("url").item(0).getTextContent();
String path = "https://www.bing.com/"; + relativePath;
System.out.println(path);
  • 结果

https://www.bing.com/az/hprichbg/rb/AuroraPhotographer_ZH-CN11480495787_1366x768.jpg

json-API(返回1920x1080分辨率)

格式format=js(返回json格式),idx=0(0代表今天,1代表昨天,最大为8),n=1(1显示一天信息,最大为8)

http://www.bing.com/HPImageArchive.aspx?format=js&;idx=0&n=1

  • json
{"images":[{"startdate":"20180628","fullstartdate":"201806281600","enddate":"20180629","url":"/az/hprichbg/rb/AuroraPhotographer_ZH-CN11480495787_1920x1080.jpg","urlbase":"/az/hprichbg/rb/AuroraPhotographer_ZH-CN11480495787","copyright":"在乌塔克雷夫海滩拍摄的北极光,挪威 (© David Clapp/Getty Images)","copyrightlink":"http://www.bing.com/search?q=%E6%9E%81%E5%85%89&form=hpcapt&mkt=zh-cn","quiz":"/search?q=Bing+homepage+quiz&filters=WQOskey:%22HPQuiz_20180628_AuroraPhotographer%22&FORM=HPQUIZ","wp":true,"hsh":"d689227d43ad344110007529ae2a7240","drk":1,"top":1,"bot":1,"hs":[]}],"tooltips":{"loading":"Loading...","previous":"Previous image","next":"Next image","walle":"This image is not available to download as wallpaper.","walls":"Download this image. Use of this image is restricted to wallpaper only."}}
  • java代码
        String strJson = getURLContent("https://www.bing.com/HPImageArchive.aspx?format=js&;idx=0&n=1") ;
        strJson="[" + strJson + "]" ;
        System.out.println(strJson) ;
        try{
            JSONArray jsonArray = JSONArray.parseArray(strJson) ;
            if(jsonArray.size() > 0 ){
                //遍历jsonArray数组,把每个对象转成json对象
                JSONObject jsonObject = jsonArray.getJSONObject(0) ;
                System.out.println(jsonObject.get("images"));
                JSONArray jsonArrays = JSONArray.parseArray(String.valueOf(jsonObject.get("images"))) ;
                for(int i = 0 ;i < jsonArrays.size() ;i ++){
                    JSONObject jsonObjects = jsonArrays.getJSONObject(i) ;
                    System.out.println("https://www.bing.com";+jsonObjects.get("url"));
                }
            }
        }catch(Exception e){

        }
  • 结果

https://www.bing.com/az/hprichbg/rb/AuroraPhotographer_ZH-CN11480495787_1920x1080.jpg

0

评论 (0)

取消