【问题标题】:Google Site Search XML API Pagination谷歌站点搜索 XML API 分页
【发布时间】:2012-08-29 05:58:24
【问题描述】:

我正在使用 Google Site Search XML API 并希望进行分页。我知道计数被认为是不准确的,但谷歌如何在http://www.google.com/sitesearch/ 的演示站点上实现他们的分页?它似乎至少可以准确地知道是否有超过 35 个结果要分成 8 个页面。

【问题讨论】:

    标签: pagination google-search google-search-api


    【解决方案1】:

    这是一个老问题,但我自己刚刚实现了这个,所以我想我应该分享一下。

    不确定您使用的是什么语言,但这是我在 PHP 中的做法($xml 当然是使用 curl 或 file_get_contents 或其他方法检索到的完整 XML 结果):

    $results_per_page = 8;
    $pages = ceil($xml->RES->M/$results_per_page);
    
    if ($pages > 1) {
       for ($i = 0; $i < $pages; $i++) { 
          $class = '';
          if ( ($i) * $results_per_page == $_GET['s']) {
              $class = 'current-page';
          }                                                    
          echo '<a href="?q=' . $searchterms . '&amp;s=' . $i * $results_per_page . '" class="pagenum '. $class . '"><strong>' . $i + 1 . '</strong></a>
       }
    }
    

    请注意,$results_per_page 应与获取的 XML url 中 num 的值匹配

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多