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

标签 java xml json bing-api

我有一个问题,因为我是 Bing Search API 的新手,不熟悉如何使用它。我正在尝试从 Bing 搜索结果中获取所有链接。所以我正在搜索关键字。正在工作,但我想获取我从包含在我的 Java 应用程序中的必应搜索 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

下面是一个检索 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/

关于java - 从 Bing Search api 获取所有链接并将它们添加到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27966272/

相关文章:

java - mirrorEnds 谜题没有给出任何错误线索

Java 到 Kotlin 在不同方法中访问类特定变量

java - 使用java xml框架创建基于XSD的xml文件

json - 通过模板将通用JSON转换为XML

.net - 在 .NET 中构建 XML 文档的最佳方法是什么?

android - 从 retrofit 响应中删除不需要的字符串

java - DOM 中不再存在缓存元素

java - REST - 从响应中获取资源 URI(不是 ClientResponse)

python - 跳过循环中的错误-JSONDecodeError : Expecting value: line 1 column 1 (char 0)

java - Spring Boot 实体中具有 OneToOne 关系的 JSON 反序列化器?