jjzjj

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

coder 2024-06-29 原文

我有一个问题,因为我是 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 - 从 Bing Search api 获取所有链接并将它们添加到数组的更多相关文章

  1. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  2. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  3. ruby - 如何以所有可能的方式将字符串拆分为长度最多为 3 的连续子字符串? - 2

    我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123

  4. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  5. ruby - 多次弹出/移动 ruby​​ 数组 - 2

    我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby​​数组,我们在StackOverflow上找到一

  6. ruby - 将数组的内容转换为 int - 2

    我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]

  7. ruby - 将 Bootstrap Less 添加到 Sinatra - 2

    我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它

  8. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

  9. ruby - 检查数组是否在增加 - 2

    这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife

  10. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

随机推荐