编程语言
首页 > 编程语言> > java – 从Bing Search api获取所有链接并将它们添加到数组中

java – 从Bing Search api获取所有链接并将它们添加到数组中

作者:互联网

我有一个问题,因为我是Bing Search API的新手,我不熟悉如何使用它.我想从Bing搜索结果中获取所有链接.所以我在搜索关键字.正在工作,但我希望获得我从我在Java应用程序中包含的Bing Search API获得的结果的链接.问题是我想检索链接并将其保存到数组中.所以我使用XML将其解析为JSON.但是当我试图获取Urls或链接时,我无法得到它们的主要问题.有没有人知道如何做或我做错了?

我想要例如获取http://en.wikipedia.org/wiki/Omonoia(Bing搜索API的搜索结果之一)

以下是一些代码:

String str = "http://maps.google.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true";
URL url = new URL(str);
InputStream is = url.openStream();
int ptr = 0;
StringBuilder builder = new StringBuilder();
while ((ptr = is.read()) != -1) {
builder.append((char) ptr);
}
String xml = builder.toString();

JSONObject jsonObject = XML.toJSONObject(xml);
System.out.println(jsonObject.toString());
System.out.println(jsonObject.get("id"));

一些输出:

"feed":{"entry":[{"id":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=' what      is omonoia'&$skip=0&$top=1","title":{"type":"text","content":"WebResult"},"updated":"2015-01-15T14:35:57Z","content":{"m:properties":{"d:Url":{"content":"http://en.wikipedia.org/wiki/AC_Omonia","m:type":"Edm.String"},"d:DisplayUrl":{"content":"en.wikipedia.org/wiki/AC_Omonia","m:type":"Edm.String"},"d:Title":{"content":"AC Omonia - Wikipedia, the free encyclopedia","m:type":"Edm.String"},"d:Description":{"content":"Athletic Club Omonoia Nicosia, commonly referred to as Omonoia, is a Cypriot professional football club based in the capital city, Nicosia. The club was established ...","m:type":"Edm.String"},"d:ID":{"content":"88cf85ab-f077-4f2b-8037-f3d3447b9d34","m:type":"Edm.Guid"}},"type":"application/xml"}},{"id":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=' what is omonoia'&$skip=1&$top=1","title":{"type":"text","content":"WebResult"},"updated":"2015-01-15T14:35:57Z","content":{"m:properties":{"d:Url":{"content":"http://en.wikipedia.org/wiki/Omonoia","m:type":"Edm.String"},"d:DisplayUrl":{"content":"en.wikipedia.org/wiki/Omonoia","m:type":"Edm.String"},"d:Title":{"content":"Omonoia - Wikipedia, the free encyclopedia","m:type":"Edm.String"},"d:Description":{"content":"Omonoia may refer to: Omonoia Square, one of Athens' main squares, Omonoia Station, the subway station located on the square or Omonoia, the neighborhood around it.","m:type":"Edm.String"},"d:ID":{"content":"4668962f-c8cb-43b1-a12d-19d8aee944bb","m:type":"Edm.Guid"}},"type":"application/xml"}},{"id":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=' what is omonoia'&$skip=2&$top=1","title":{"type":"text","content":"WebResult"},"updated":"2015-01-15T14:35:57Z","content":{"m:properties":{"d:Url":{"content":"http://wikitravel.org/en/Athens/Omonia","m:type":"Edm.String"},"d:DisplayUrl":{"content":"wikitravel.org/en/Athens/Omonia","m:type":"Edm.String"},"d:Title":{"content":"Athens/Omonia - Wikitravel - The Free Travel Guide","m:type":"Edm.String"},"d:Description":{"content":"Omonia Square is the center of Athens, and is composed of the actual square together with the surrounding streets, open areas and assemblage of grand buildings that ...","m:type":"Edm.String"},"d:ID":{"content":"b711b509-d478-48c4-ad44-51e9d87a5646","m:type":"Edm.Guid"}},"type":"application/xml"}},{"id":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=' what is omonoia'&$skip=3&$top=1","title":{"type":"text","content":"WebResult"},"updated":"2015-01-15T14:35:57Z","content":{"m:properties":{"d:Url":{"content":"http://www.youtube.com/watch?v=nZfkY7b5vIo","m:type":"Edm.String"},"d:DisplayUrl":{"content":"www.youtube.com/watch?v=nZfkY7b5vIo","m:type":"Edm.String"},"d:Title":{"content":"OMONOIA Vs ANORTHOSI 3-2 - YouTube","m:type":"Edm.String"},"d:Description":{"content":"OMONOIA Vs ANORTHOSI 3-2 - YouTube ... YouTube home","m:type":"Edm.String"},"d:ID":{"content":"a8870310-3a66-493e-9155-3608501305d2","m:type":"Edm.Guid"}},"type":"application/xml"}},{"id":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=' what is omonoia'&$skip=4&$top=1","title":{"type":"text","content":"WebResult"},"updated":"2015-01-15T14:35:57Z","content":{"m:properties":{"d:Url":{"content":"http://www.youtube.com/watch?v=EOg6rMweu38","m:type":"Edm.String"},"d:DisplayUrl":{"content":"www.youtube.com/watch?v=EOg6rMweu38","m:type":"Edm.String"},"d:Title":{"content":"OMONOIA Vs APOLLON 2-4 - YouTube","m:type":"Edm.String"},"

我从这个问题得到了一些源代码:Parsing external XML to JSON in Java?

解决方法:

您可以指定格式,因此不必使用XML然后解析为JSON.要获取JSON,只需在URL中通知$format选项,如下所示:

https://api.datamarket.azure.com/Bing/SearchWeb/Web?Query='what      is omonoia'&$format=json

下面是一个检索网址的示例:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.Base64;

import org.json.JSONArray;
import org.json.JSONObject;

public class BingSearchApiSample {

    public static void main(final String[] args) throws Exception {
        final String accountKey = "<Your Bing API Key>";
        final String bingUrlPattern = "https://api.datamarket.azure.com/Bing/Search/Web?Query=%%27%s%%27&$format=JSON";

        final String query = URLEncoder.encode("'what      is omonoia'", Charset.defaultCharset().name());
        final String bingUrl = String.format(bingUrlPattern, query);

        final String accountKeyEnc = Base64.getEncoder().encodeToString((accountKey + ":" + accountKey).getBytes());

        final URL url = new URL(bingUrl);
        final URLConnection connection = url.openConnection();
        connection.setRequestProperty("Authorization", "Basic " + accountKeyEnc);

        try (final BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
            String inputLine;
            final StringBuilder response = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            final JSONObject json = new JSONObject(response.toString());
            final JSONObject d = json.getJSONObject("d");
            final JSONArray results = d.getJSONArray("results");
            final int resultsLength = results.length();
            for (int i = 0; i < resultsLength; i++) {
                final JSONObject aResult = results.getJSONObject(i);
                System.out.println(aResult.get("Url"));
            }
        }
    }

}

编辑:JSON内容在此链接中显示:http://pastebin.com/TcGg6SzN

编辑:该示例生成以下输出:

http://en.wikipedia.org/wiki/AC_Omonia
http://en.wikipedia.org/wiki/Omonoia
http://www.youtube.com/watch?v=XDTjbljYoM8
http://wikitravel.org/en/Athens/Omonia
http://www.youtube.com/watch?v=JwwuQgg3O8o
http://www.omonoia.com.cy/
https://www.omonoia.com.cy/
http://www.athensguide.com/omonia.html
http://wn.com/This_Is_Omonoia
http://www.greece-athens.com/metro/omonoia.php
http://www.encyclo.co.uk/meaning-of-Omonoia
http://www.flickr.com/photos/mascarpone/sets/72157611666181729/
http://pt.wikipedia.org/wiki/Pra%C3%A7a_Omonia
http://en.m.wikipedia.org/wiki/Omonoia,_Athens
http://omonoia.info/
http://www.urbandictionary.com/define.php?term=omonoia
http://www.omonia.org/omonoia/Fair_Play.shtml
https://www.linkedin.com/pub/omonoia-omonoia/11/8b2/695
http://www.ustream.tv/channel/ael---omonoia
http://www.omonia.org/about.shtml
http://www.cyclopaedia.info/wiki/Omonoia
http://www.thisisathens.org/taxonomy/term/15
http://allochiria.bandcamp.com/album/omonoia
http://www.dvbs.eu.org/omonoia/
http://www.facebook.com/pages/omonoia/307148722634077
Homepage
<iframe class="wp-embedded-content" data-secret="p2XsNlcr3C" frameborder="0" height="282" marginheight="0" marginwidth="0" sandbox="allow-scripts" scrolling="no" security="restricted" src="http://omonoianews.com/embed/#?secret=p2XsNlcr3C" style="position: absolute; clip: rect(1px, 1px, 1px, 1px);" title="“Homepage” — Omonoianews.com" width="500"></iframe> http://www.eurobasket.com/team.asp?Cntry=CYP&Team=1019 http://worddomination.com/omonoia.html <iframe frameborder="0" height="250" src="https://www.mixcloud.com/widget/follow/?u=https%3A%2F%2Fwww.mixcloud.com%2Fantreas-omonoia%2F" width="200"></iframe> http://www.newhois.net/www/omonoia.com.cy.html http://omonoiany.com/html/crete.html http://vigorito.com.br/codigos/omonoia-fc http://www.facebook.com/pages/OMONOIA/118545104824439 http://www.cyclopaedia.info/wiki/Omonoia-1 http://www.vipfilefinder.com/download/omonoia/ http://new.livestream.com/accounts/380859 http://www.sevodnya.com/omonoia/ http://www.answers.com/Q/When_was_Omonoia_-_organization_-_created http://omonoia.com.cubestat.com/ https://twitter.com/eurovison http://omonoialinks.com/?source=7&date=yes http://members.tripod.com/antonis_antoniou/season9900/omoapoeE.html http://www.quickiwiki.com/en/Omonoia,_Athens https://www.torrentz.com/search?q=omonoia http://www.omonoia.org/ http://omonoia.com.cy.onlinenoffline.com/ http://omonoia.com.cy.hypestat.com/ http://wn.com/Omonoia__This_Is_My_Life http://new.livestream.com/accounts/8561914 http://omonoia-nafpaktou.gr.ipaddress.com/

标签:bing-api,java,json,xml
来源: https://codeday.me/bug/20190830/1766297.html