从 Linux 终端进行 Google 搜索

Dea*_*her 35 terminal command-line linux-terminal

我看到有人在不久前编写了一个 Google 控制台应用程序,但它实际上是一个模拟控制台的网站。

我想要的是一个快捷方式或 Linux 终端应用程序,我可以用它来快速搜索 Google。

理想情况下,它会显示前 10 个搜索结果,旁边带有数字,按下数字将在浏览器中打开该网站。

在浏览器中打开 Google 结果也很好。

有没有人有办法解决吗?

inn*_*naM 26

这是一个简单的 bash 函数,可让您键入

google foo bar
Run Code Online (Sandbox Code Playgroud)

然后将打开您的默认浏览器以显示这些搜索词的 Google 结果页面:

google() {
    search=""
    echo "Googling: $@"
    for term in $@; do
        search="$search%20$term"
    done
    xdg-open "http://www.google.com/search?q=$search"
}
Run Code Online (Sandbox Code Playgroud)

只需将其粘贴到您的终端中即可尝试。

对于 Windows 或 Mac OS X,将最后一行替换为以下之一(假设您在 Windows 上使用 Cygwin 或类似工具):

视窗

start "http://www.google.com/search?q=$search"
Run Code Online (Sandbox Code Playgroud)

Mac OS X

open "http://www.google.com/search?q=$search"
Run Code Online (Sandbox Code Playgroud)

  • 我通过将 xdg-open 更改为 open,删除功能部分,然后将脚本添加到 bin 将其变成了 mac 的 shell 脚本。谢谢! (2认同)

Wer*_*ght 11

google-cli应该做到这一点(它是cli-google的复兴版本)。


小智 10

#!/bin/bash

if [[ $(echo $*) ]]; then

    searchterm="$*"

else

    read -p "Enter your search term: " searchterm

fi

searchterm=$(echo $searchterm | sed -e 's/\ /+/g')

lynx -dump http://www.google.com/search?q=$searchterm | less
Run Code Online (Sandbox Code Playgroud)

将此脚本复制并粘贴到 中~/bin,将其命名为“鹅”或其他名称(GOOgle 搜索)。修改它 +x

用法是:

goose searchterm
Run Code Online (Sandbox Code Playgroud)

显然,您必须安装 Lynx。


bel*_*qua 9

如果您想从命令行搜索并跳转到用户可定义的浏览器以获取结果,另一种解决方案是使用surfraw

   Surfraw  provides  a fast unix command line interface to a variety of
   popular WWW search engines and other artifacts of power.  It reclaims
   google,  altavista, dejanews, freshmeat, research index, slashdot and
   many others from the false?prophet,  pox?infested  heathen  lands  of
   html?forms,  placing  these  wonders  where they belong, deep in unix
   heartland, as god loving extensions to the shell.
Run Code Online (Sandbox Code Playgroud)

它在某些 Linux 发行版(Debian、Ubuntu、未知的其他发行版)中提供,来自 debian.org 的源代码,最新的开发代码和版本可从官方 git 存储库(现在位于 GitLab 上)获得。

来自 tarball 或 deb 文件的安装说明可在Wiki上找到。

小贴士:Surfraw 最初是由 Julian Assange 编写的。(“SURFRAW”的首字母缩略词/反义词是壳牌用户对万维网的革命性前线愤怒。)

要从命令行进行 Google 搜索:
sr google archibald tuttle

除了普通的旧 Google 之外,还有许多其他内置搜索类型。

要搜索处理 S/MIME 的 RFC:
sr rfc s/mime

翻译一句话:
sr translate logiciel

查找种子:
sr piratebay free music

(这些关键字搜索类型会继续更新。)

更高级的用法

     $ surfraw google -results=100 RMS, GNU, which is sinner, which is sin?
     $ sr wikipedia surfraw
     $ sr austlii -method=phrase dog like
     $ /usr/lib/surfraw/rhyme -method=perfect Julian
Run Code Online (Sandbox Code Playgroud)

Surfraw 是可配置的。您可以使用一些默认值进行设置,无论是每个用户输入$HOME/.surfraw.conf还是系统范围输入/etc/surfraw.conf

SURFRAW_graphical_browser="/usr/bin/links2 -g"
SURFRAW_text_browser="/usr/bin/elinks"
SURFRAW_graphical=yes
Run Code Online (Sandbox Code Playgroud)

在这里,我将其设置为使用 links2 和 elinks,但您可以使用 Firefox、Chrome 或任何其他您喜欢的工具。

(Nb.links2 -g是一个奇怪的终端嵌入式图形模式浏览器。它很快,但讨厌现代性。)


小智 5

wget, 为例:

wget -U 'Firefox/3.0.15' http://www.google.com/search?q=wget+google+query+to+file -O file.html
Run Code Online (Sandbox Code Playgroud)

来源