以编程方式搜索Google

时间:2011-10-17 11:14:43

标签: search

有没有人知道是否以及如何在不在搜索框中输入查询的情况下以编程方式搜索Google?我想双击网页中的某个字词并获取Google搜索结果。

2 个答案:

答案 0 :(得分:2)

您只需链接到网址:

http://www.google.com/search?q=query+here

非常简单。此外,如果您使用.com自动重定向到用户自己的语言专用页面,除非您在lang=参数中指定语言。

答案 1 :(得分:0)

你可以在大多数网页浏览器中创建一个书签,AKA,一个bookmarklet,因为它实际上是JavaScript代码:

javascript:window.open("https://www.google.com/search?q=" +
encodeURIComponent(window.getSelection()).substr(0,2000), "_self");

您可以将Google替换为Bing或任何首选搜索引擎,例如

javascript:window.open("http://www.exalead.com/search/web/results/?q=" +
encodeURIComponent(window.getSelection()).substr(0,2000), "_self");

将在欧洲竞争对手Exalead上打开一个查询。

将其分解,JavaScript代码执行以下操作

javascript:window.open( // we will be opening  a link
    "https://www.google.com/search?q=" + // we will be using Google as our base search engine
    encodeURIComponent( // we have to convert spaces to %20 and change rest of string to encoded URI specs
        window.getSelection()).substr(0,2000), // get the selected text and grab first 2000 characters of encoded string
    // since Google caps string queries at 2048 characters - you may have to shorten this slightly
    "_self"); // open in same tab - use "_blank" or other desired target
相关问题