【问题标题】:Get all the Links from Bing Search api and add them to array从 Bing Search api 获取所有链接并将它们添加到数组
【发布时间】:2015-03-14 00:19:55
【问题描述】:

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

例如我想获取http://en.wikipedia.org/wiki/Omonoia(必应搜索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?

【问题讨论】:

    标签: java xml json bing-api


    【解决方案1】:

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

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

    下面是一个检索 url 的示例:

    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
    http://omonoianews.com/
    http://www.eurobasket.com/team.asp?Cntry=CYP&Team=1019
    http://worddomination.com/omonoia.html
    http://www.mixcloud.com/antreas-omonoia/
    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/
    

    【讨论】:

    • 我尝试这样做,但我收到此错误:org.json.JSONException: JSONObject["d"] not found。在 org.json.JSONObject.get(JSONObject.java:454) 在 org.json.JSONObject.getJSONObject(JSONObject.java:553)....
    • @user4457996 响应的内容是什么?你替换 ?
    • 是的,我替换了它。但我认为需要获取 url 或任何标签的命令是错误的..
    • @user4457996 编辑了带有结果的示例。试试看response的内容。
    • 注意URL已经改了,所以需要把../Search/Web...改成.../SearchWeb/v1/Web...
    猜你喜欢
    • 2021-06-05
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    相关资源
    最近更新 更多