【问题标题】:Searching using the Google custom search API and displaying links使用 Google 自定义搜索 API 进行搜索并显示链接
【发布时间】:2014-04-15 20:37:04
【问题描述】:

我正在开发用于家庭自动化的个人助理,到目前为止,它具有搜索 wolfram alpha 和提取天气条件/预测等基本功能,但我不希望它能够在 google 上搜索内容并将结果显示在屏幕。

在社区中搜索后,似乎推荐的方法是使用 Google Search API(已被 Google Custom Search API 取代。所以我查看了一些示例并且能够将数据导出到数据网格中然而,在 windows 窗体上。我想显示可点击的链接。我该怎么做?我已经有一个 API 密钥和 CX 可与代码一起使用,但无法获得正确的输出。

GoogleSearch search = new GoogleSearch()
        {
            Key = "KEY HERE",
            CX = "CX HERE"
        };
        search.SearchCompleted += (a, b) =>
        {
            this.DataGridResults.ItemsSource = b.Response.Items;
        };
        search.Search(search_query.Text);

【问题讨论】:

  • Regex 生成可点击链接怎么样?如果这是唯一的问题。
  • 我也不完全确定如何从数据网格中获取个人信息,所以我猜这是我的主要问题。虽然我确信有一种更简单的方法可以做到这一点。

标签: c# api search


【解决方案1】:

所以我在研究了很长时间后解决了这个问题。原来我只是使用该方法返回错误的列表。我附上了原始帖子的链接,该链接为我提供了方法和完整的解决方案,该解决方案仅在文本框中输出标题和 HTML 链接。你可以从那里对他们做任何你喜欢的事情。

private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        GoogleSearch search = new GoogleSearch()
        {
            Key = "API KEY HERE",
            CX = "CX GOES HERE"
        };
        search.SearchCompleted += (a, b) =>
        {
            foreach (Item i in b.Response.Items)
            {
                results_box.Text = results_box.Text + Environment.NewLine + "Page Title: " + i.Title;
                results_box.Text = results_box.Text + Environment.NewLine + "Link to Page " + i.Link;

            };
        };
        search.Search(search_query.Text);

方法和原帖可以在http://kiwigis.blogspot.com/2011/03/google-custom-search-in-c.html找到

【讨论】:

    猜你喜欢
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多