使用 Java 的 Bing 新闻搜索 API

发布于 2024-11-02 11:24:37 字数 4601 浏览 0 评论 0 原文

我正在尝试使用 bing-search-java-api 的 NewsSample.java 返回特定搜索词的热门新闻结果。

public class NewsSample {
private static final String APPLICATION_KEY_OPTION = "47662CD8468093923CEC71C4EFA8342775BC589G";

/** The Constant QUERY_OPTION. */
private static final String QUERY_OPTION = "google";

/** The Constant HELP_OPTION. */
private static final String HELP_OPTION = "help";

/**
 * The main method.
 * 
 * @param args the arguments
 */
public static void main(String[] args) {
    Options options = buildOptions();
    try {
        CommandLine line = new BasicParser().parse(options, args);
        processCommandLine(line, options);
    } catch(ParseException exp ) {
        System.err.println(exp.getMessage());
        printHelp(options);
    }
}

/**
 * Process command line.
 * 
 * @param line the line
 * @param options the options
 */
private static void processCommandLine(CommandLine line, Options options) {
    if(line.hasOption(HELP_OPTION)) {
        printHelp(options);            
    } else if(line.hasOption(APPLICATION_KEY_OPTION) && line.hasOption(QUERY_OPTION)) {
        BingSearchServiceClientFactory factory = BingSearchServiceClientFactory.newInstance();
        BingSearchClient client = factory.createBingSearchClient();
        SearchResponse response = client.search(createSearchRequest(client, line.getOptionValue(APPLICATION_KEY_OPTION), line.getOptionValue(QUERY_OPTION)));
        printResponse(response);
    } else {
        printHelp(options);
    }
}

/**
 * Builds the options.
 * 
 * @return the options
 */
private static Options buildOptions() {

    Options opts = new Options();

    String helpMsg = "Print this message.";
    Option help = new Option(HELP_OPTION, helpMsg);
    opts.addOption(help);

    String applicationKeyMsg = "You Application ID.";
    OptionBuilder.withArgName("appid");
    OptionBuilder.hasArg();
    OptionBuilder.withDescription(applicationKeyMsg);
    Option applicationKey = OptionBuilder.create(APPLICATION_KEY_OPTION);
    opts.addOption(applicationKey);

    String queryMsg = "Search Query.";
    OptionBuilder.withArgName("query");
    OptionBuilder.hasArg();
    OptionBuilder.withDescription(queryMsg);
    Option query = OptionBuilder.create(QUERY_OPTION);
    opts.addOption(query);

    return opts;
}

/**
 * Prints the help.
 * 
 * @param options the options
 */
private static void printHelp(Options options) {
    int width = 80;
    String syntax = NewsSample.class.getName() + " <options>";
    String header = MessageFormat.format("\nThe -{0} and -{1} options are required. All others are optional.", APPLICATION_KEY_OPTION, QUERY_OPTION);
    String footer = "";
    new HelpFormatter().printHelp(width, syntax, header, options, footer, false);
}

/**
 * Prints the response.
 * 
 * @param response the response
 */
private static void printResponse(SearchResponse response) {
    System.out.println("Bing API Version " + response.getVersion());
    System.out.println("News results for " + response.getQuery().getSearchTerms());

    for (NewsResult result : response.getNews().getResults()) {
        System.out.println(result.getTitle());
        System.out.println(result.getUrl());
        System.out.println(result.getSource());
        System.out.println(result.getDate());
        System.out.println(result.getSnippet());
        System.out.println();
    }
}

/**
 * Creates the search request.
 * 
 * @param client the client
 * @param applicationId the application id
 * @param query the query
 * 
 * @return the search request
 */
private static SearchRequest createSearchRequest(BingSearchClient client, String applicationId, String query) {
    SearchRequestBuilder builder = client.newSearchRequestBuilder();
    builder.withAppId(applicationId);
    builder.withQuery(query);
    builder.withSourceType(SourceType.NEWS);
    builder.withVersion("2.0");
    builder.withMarket("en-us");
    builder.withSearchOption(SearchOption.ENABLE_HIGHLIGHTING);

    builder.withNewsRequestOffset(0L);
    builder.withNewsRequestCategory("rt_Political");
    builder.withNewsRequestSortOption(NewsSortOption.RELEVANCE);
    return builder.getResult();
}

然而,

我得到的唯一响应是...

        usage: com.Bing.WebSample <options>

The -47662CD8468093923CEC71C4EFA8342775BC589G and -google options are required.
All others are optional.
 -47662CD8468093923CEC71C4EFA8342775BC589G <appid>   You Application ID.
 -google <query>                                     Search Query.
 -help

是否有人使用过 Bing 搜索 API 并知道如何获得实际响应? 谢谢。

I am trying to use the bing-search-java-api's NewsSample.java to return top news results for a certain search term.

public class NewsSample {
private static final String APPLICATION_KEY_OPTION = "47662CD8468093923CEC71C4EFA8342775BC589G";

/** The Constant QUERY_OPTION. */
private static final String QUERY_OPTION = "google";

/** The Constant HELP_OPTION. */
private static final String HELP_OPTION = "help";

/**
 * The main method.
 * 
 * @param args the arguments
 */
public static void main(String[] args) {
    Options options = buildOptions();
    try {
        CommandLine line = new BasicParser().parse(options, args);
        processCommandLine(line, options);
    } catch(ParseException exp ) {
        System.err.println(exp.getMessage());
        printHelp(options);
    }
}

/**
 * Process command line.
 * 
 * @param line the line
 * @param options the options
 */
private static void processCommandLine(CommandLine line, Options options) {
    if(line.hasOption(HELP_OPTION)) {
        printHelp(options);            
    } else if(line.hasOption(APPLICATION_KEY_OPTION) && line.hasOption(QUERY_OPTION)) {
        BingSearchServiceClientFactory factory = BingSearchServiceClientFactory.newInstance();
        BingSearchClient client = factory.createBingSearchClient();
        SearchResponse response = client.search(createSearchRequest(client, line.getOptionValue(APPLICATION_KEY_OPTION), line.getOptionValue(QUERY_OPTION)));
        printResponse(response);
    } else {
        printHelp(options);
    }
}

/**
 * Builds the options.
 * 
 * @return the options
 */
private static Options buildOptions() {

    Options opts = new Options();

    String helpMsg = "Print this message.";
    Option help = new Option(HELP_OPTION, helpMsg);
    opts.addOption(help);

    String applicationKeyMsg = "You Application ID.";
    OptionBuilder.withArgName("appid");
    OptionBuilder.hasArg();
    OptionBuilder.withDescription(applicationKeyMsg);
    Option applicationKey = OptionBuilder.create(APPLICATION_KEY_OPTION);
    opts.addOption(applicationKey);

    String queryMsg = "Search Query.";
    OptionBuilder.withArgName("query");
    OptionBuilder.hasArg();
    OptionBuilder.withDescription(queryMsg);
    Option query = OptionBuilder.create(QUERY_OPTION);
    opts.addOption(query);

    return opts;
}

/**
 * Prints the help.
 * 
 * @param options the options
 */
private static void printHelp(Options options) {
    int width = 80;
    String syntax = NewsSample.class.getName() + " <options>";
    String header = MessageFormat.format("\nThe -{0} and -{1} options are required. All others are optional.", APPLICATION_KEY_OPTION, QUERY_OPTION);
    String footer = "";
    new HelpFormatter().printHelp(width, syntax, header, options, footer, false);
}

/**
 * Prints the response.
 * 
 * @param response the response
 */
private static void printResponse(SearchResponse response) {
    System.out.println("Bing API Version " + response.getVersion());
    System.out.println("News results for " + response.getQuery().getSearchTerms());

    for (NewsResult result : response.getNews().getResults()) {
        System.out.println(result.getTitle());
        System.out.println(result.getUrl());
        System.out.println(result.getSource());
        System.out.println(result.getDate());
        System.out.println(result.getSnippet());
        System.out.println();
    }
}

/**
 * Creates the search request.
 * 
 * @param client the client
 * @param applicationId the application id
 * @param query the query
 * 
 * @return the search request
 */
private static SearchRequest createSearchRequest(BingSearchClient client, String applicationId, String query) {
    SearchRequestBuilder builder = client.newSearchRequestBuilder();
    builder.withAppId(applicationId);
    builder.withQuery(query);
    builder.withSourceType(SourceType.NEWS);
    builder.withVersion("2.0");
    builder.withMarket("en-us");
    builder.withSearchOption(SearchOption.ENABLE_HIGHLIGHTING);

    builder.withNewsRequestOffset(0L);
    builder.withNewsRequestCategory("rt_Political");
    builder.withNewsRequestSortOption(NewsSortOption.RELEVANCE);
    return builder.getResult();
}

}

However the only response I get is...

        usage: com.Bing.WebSample <options>

The -47662CD8468093923CEC71C4EFA8342775BC589G and -google options are required.
All others are optional.
 -47662CD8468093923CEC71C4EFA8342775BC589G <appid>   You Application ID.
 -google <query>                                     Search Query.
 -help

Has anyone ever worked with the Bing Search API that knows how to get an actual response?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

未央 2024-11-09 11:24:37

必应新闻搜索对我来说效果很好。为我运行的代码示例是:

        BingSearchServiceClientFactory factory = BingSearchServiceClientFactory.newInstance();
    BingSearchClient client = factory.createBingSearchClient();

    SearchRequestBuilder builder = client.newSearchRequestBuilder();
    builder.withAppId(appId);
    builder.withQuery(query);
    builder.withSourceType(SourceType.NEWS);
    builder.withVersion("2.0");
    builder.withMarket("en-us");
    builder.withAdultOption(AdultOption.STRICT);
    builder.withSearchOption(SearchOption.ENABLE_HIGHLIGHTING);

    builder.withNewsRequestOffset(0L);
    builder.withNewsRequestCount(10L);

    SearchResponse response = client.search(builder.getResult());

需要设置的两个属性是 appId 和 query,这是要使用的查询术语。我认为问题一定是由您尝试从用户输入中收集这两个值的代码的命令行部分引起的。

不管幕后发生了什么,Bing 搜索 API 确实运行得相当好。我对 Bing 或 Google 方法都没有既得利益,但我仍然对 3 个月前(截至撰写本文时)Google 的翻译 API 被弃用感到不安。

The Bing News Search works fine for me. The code example which runs for me is:

        BingSearchServiceClientFactory factory = BingSearchServiceClientFactory.newInstance();
    BingSearchClient client = factory.createBingSearchClient();

    SearchRequestBuilder builder = client.newSearchRequestBuilder();
    builder.withAppId(appId);
    builder.withQuery(query);
    builder.withSourceType(SourceType.NEWS);
    builder.withVersion("2.0");
    builder.withMarket("en-us");
    builder.withAdultOption(AdultOption.STRICT);
    builder.withSearchOption(SearchOption.ENABLE_HIGHLIGHTING);

    builder.withNewsRequestOffset(0L);
    builder.withNewsRequestCount(10L);

    SearchResponse response = client.search(builder.getResult());

The two attributes needed setting are appId and query, which is query term to use. I think the problem must result from the command line part of the code in which you are trying to gather those two values from the user input.

Regardless of what is going on behind the scenes, the Bing search API does work fairly well. I have no vested interest in either the Bing or the Google approach, but I am still upset over the translation API of Google being deprecated 3 months back (as of the time of writing).

我不会写诗 2024-11-09 11:24:37

我不知道您是否有使用 Bing 的具体要求,Google也为开发者提供了一套非常好的工具。

是 Google 搜索引擎 API 的链接(如果您感兴趣的话)。

I don't know if you have a specific requirement to use Bing, but you could just cut out the middle man and use google. Bing does. Google has a really good set of tools available to devs as well.

This is a link to googles search engine api (if you were interested).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文