自定义Google搜索视图

您知道每次从网络下载多少数据才能看到此页面吗?



如果没有浏览器缓存,则为600 KB(在gzip-e中)和38个请求。 如果使用缓存,则需要70 KB和7-8个请求。



你知道什么是600 KB吗? 这些都是在同一gzip-e中的托尔斯泰的《战争与和平》。

当然,令人高兴的是Google Inc. 向我们填充了很多有用的信息。 您甚至不必讨论搜索结果本身-这里是图片,维基百科的背景信息,开放时间,地图和评论。 但是有时候您想要简单明了的东西。 没有强奸链接,也没有出售您的网络广告系列以将主要市场推销第三方营销商。 不要放慢过时的智能手机和拥挤的移动网络的速度。

当然,您可以通过自己的脚本查询搜索引擎,其中包括不需要的所有内容,但幸运的是,Google本身提供了一种优雅的解决方案-使用纯JSON或XML格式的结果访问其Search API。

这是示例的JSON响应的一部分:

杰森
{ "kind": "customsearch#search", "url": { "type": "application/json", "template": "https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json" }, "queries": { "request": [ { "title": "Google Custom Search - habr", "totalResults": "327000", "searchTerms": "habr", "count": 10, "startIndex": 1, "inputEncoding": "utf8", "outputEncoding": "utf8", "safe": "off", "cx": "0019687164026253:oi9ssr7ci6e" } ], "nextPage": [ { "title": "Google Custom Search - habr", "totalResults": "327000", "searchTerms": "habr", "count": 10, "startIndex": 11, "inputEncoding": "utf8", "outputEncoding": "utf8", "safe": "off", "cx": "00156687164026253:oi9ssr7ci6e" } ] }, "context": { "title": "v-gornom.ru" }, "searchInformation": { "searchTime": 0.531851, "formattedSearchTime": "0,53", "totalResults": "327000", "formattedTotalResults": "327 000" }, "items": [ { "kind": "customsearch#result", "title": "    / ", "htmlTitle": "    / \u003cb\u003e\u003c/b\u003e", "link": "https://habr.com/", "displayLink": "habr.com", "snippet": " –      ,    \n .  ,  , ...", "htmlSnippet": "\u003cb\u003e\u003c/b\u003e –      ,    \u003cbr\u003e\n .  ,  , ...", "cacheId": "Wg3CgcvcAxUJ", "formattedUrl": "https://habr.com/", "htmlFormattedUrl": "https://\u003cb\u003ehabr\u003c/b\u003e.com/", "pagemap": { "cse_thumbnail": [ { "width": "311", "height": "162", "src": "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcT9MbmejtANdWSuXxH7pxKqWNFFRzPiTQuLmhWk5Tc5-Y-AgUgPrwEz0MV9" } ], "metatags": [ { "viewport": "width=1024", "fb:app_id": "444736788986613", "og:type": "website", "fb:pages": "472597926099084", "og:site_name": "", "og:image": "https://habr.com/images/habr.png", "og:image:width": "1200", "og:image:height": "628", "og:title": "    / ", "og:description": " –      ,     .  ,  ,      –       IT-  .", "yandex-verification": "71593b225aeafc4e", "referrer": "unsafe-url", "pocket-site-verification": "ed24b2b9721edf0a282c5b4a3232c4", "application-name": "", "msapplication-tilecolor": "#77a2b6", "theme-color": "#77a2b6" } ], "cse_image": [ { "src": "https://habr.com/images/habr.png" } ] } }, { "kind": "customsearch#result", "title": " — ", "htmlTitle": "\u003cb\u003e\u003c/b\u003e — ", "link": "https://ru.wikipedia.org/wiki/%D0%A5%D0%B0%D0%B1%D1%80", "displayLink": "ru.wikipedia.org", "snippet": " ( ) —  -   \n     ,   \n ...", "htmlSnippet": "\u003cb\u003e\u003c/b\u003e ( \u003cb\u003e\u003c/b\u003e) —  -   \u003cbr\u003e\n     ,   \u003cbr\u003e\n ...", "cacheId": "lB7MkUOxB7gJ", "formattedUrl": "https://ru.wikipedia.org/wiki/", "htmlFormattedUrl": "https://ru.wikipedia.org/wiki/\u003cb\u003e\u003c/b\u003e", "pagemap": { "cse_thumbnail": [ { "width": "220", "height": "130", "src": "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQqJU0itnwYGZAd6Z5aMJO0izPfF7cab6pc40vm0sujOP2dSgkP-UB0Je1T" } ], "metatags": [ { "referrer": "origin", "og:image": "https://upload.wikimedia.org/wikipedia/ru/7/7f/Habrahabr_logo.png" } ], "cse_image": [ { "src": "https://upload.wikimedia.org/wikipedia/ru/7/7f/Habrahabr_logo.png" } ] } }, 


在本文中,我们将为Google自定义搜索创建一个视图,该视图将具有Google的功能并满足我们的平凡愿望。

要访问Google自定义搜索API,您首先需要获取搜索引擎的标识符。 为此,请在cse.google.com上注册自定义搜索引擎。

我们转到该站点,添加搜索引擎,指定任意站点,转到控制面板,然后获取搜索引擎的标识符。

现在,您需要获取令牌才能访问自定义搜索API。

我们转到console.cloud.google.com (假设您已经在两个站点上都拥有帐户),创建一个项目,在左侧菜单中选择“ API和服务”→“控制面板”→“启用API和服务”,查找有“自定义搜索API”,选择,启用然后创建凭据-即“ API密钥”。 您只能将此密钥的使用限制为您的IP,而不能这样做。

仅此而已。 现在,我们可以通过以下行发送Google请求:

https://www.googleapis.com/customsearch/v1?key={apikey}&cx={sid}&q=query

其中{apikey}是访问令牌,{sid}是搜索标识符。

作为响应,我们得到了一个干净的JSON。

使用简单的php脚本,即使原始视图有些近似我们也可以得到View。 第一页:

搜索结果:



第一页为2Kb,搜索结果为6Kb。

这里的重点不仅是极简主义。 Google在JSON响应中提供了一堆有趣的信息,并允许您使用至少一堆参数指定请求。 更多细节在这里 。 您甚至可以执行搜索引擎主页不允许的操作。

当然也有缺点-每天最多100个请求。 其余的是美元。

顺便说一句,您也可以在Google上托管我们的php脚本,并且绝对免费-在https://appengine.google.com上 。 没错,这里您已经遭受了很多麻烦-FTP,Web文件管理器以及Google入门网站的根目录。 因此,您将必须将php脚本转换为应用程序,将其放入引擎中,并使用特殊的控制台将其部署到云中。

实际上,这项任务并不简单。 难怪Google会推出新的认证-Google认证用户。

Source: https://habr.com/ru/post/zh-CN421173/


All Articles