linux - 使用命令行从谷歌下载图像

标签 linux shell web

<分区>

我想下载谷歌通过命令行给我的第 n 个图像,例如使用命令 wget

要搜索 [something] 的图像,我只需转到页面 https://www.google.cz/search?q=[something]&tbm=isch 但是我如何获得第 n 个搜索结果的 url 以便我可以使用 wget?

最佳答案

第一次尝试

首先您需要设置用户代理,以便谷歌授权搜索输出。然后我们可以查找图像并选择所需的图像。为了完成我们插入缺失的换行符,wget 将在一行中返回谷歌搜索,并过滤链接。文件的索引存储在变量 count 中。

$ count=10
$ imagelink=$(wget --user-agent 'Mozilla/5.0' -qO - "www.google.be/search?q=something\&tbm=isch" | sed 's/</\n</g' | grep '<img' | head -n"$count" | tail -n1 | sed 's/.*src="\([^"]*\)".*/\1/')
$ wget $imagelink 

图像现在将位于您的工作目录中,您可以调整最后一个命令并指定所需的输出文件名。

你可以用一个shell脚本总结一下:

#! /bin/bash
count=${1}
shift
query="$@"
[ -z $query ] && exit 1  # insufficient arguments
imagelink=$(wget --user-agent 'Mozilla/5.0' -qO - | "www.google.be/search?q=${query}\&tbm=isch" | sed 's/</\n</g' | grep '<img' | head -n"$count" | tail -n1 | sed 's/.*src="\([^"]*\)".*/\1/')
wget -qO google_image $imagelink

示例用法:

$ ls
Documents
Downloads
Music
script.sh
$ chmod +x script.sh
$ bash script.sh 5 awesome
$ ls
Documents
Downloads
google_image
Music
script.sh

现在 google_image 应该在查找“awesome”时包含第五张 google 图片。如果您遇到任何错误,请告诉我,我会处理的。

更好的代码

此代码的问题在于它以低分辨率返回图片。更好的解决方案如下:

#! /bin/bash

# function to create all dirs til file can be made
function mkdirs {
    file="$1"
    dir="/"

    # convert to full path
    if [ "${file##/*}" ]; then
        file="${PWD}/${file}"
    fi

    # dir name of following dir
    next="${file#/}"

    # while not filename
    while [ "${next//[^\/]/}" ]; do
        # create dir if doesn't exist
        [ -d "${dir}" ] || mkdir "${dir}"
        dir="${dir}/${next%%/*}"
        next="${next#*/}"
    done

    # last directory to make
    [ -d "${dir}" ] || mkdir "${dir}"
}

# get optional 'o' flag, this will open the image after download
getopts 'o' option
[[ $option = 'o' ]] && shift

# parse arguments
count=${1}
shift
query="$@"
[ -z "$query" ] && exit 1  # insufficient arguments

# set user agent, customize this by visiting http://whatsmyuseragent.com/
useragent='Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:31.0) Gecko/20100101 Firefox/31.0'

# construct google link
link="www.google.cz/search?q=${query}\&tbm=isch"

# fetch link for download
imagelink=$(wget -e robots=off --user-agent "$useragent" -qO - "$link" | sed 's/</\n</g' | grep '<a href.*\(png\|jpg\|jpeg\)' | sed 's/.*imgurl=\([^&]*\)\&.*/\1/' | head -n $count | tail -n1)
imagelink="${imagelink%\%*}"

# get file extention (.png, .jpg, .jpeg)
ext=$(echo $imagelink | sed "s/.*\(\.[^\.]*\)$/\1/")

# set default save location and file name change this!!
dir="$PWD"
file="google image"

# get optional second argument, which defines the file name or dir
if [[ $# -eq 2 ]]; then
    if [ -d "$2" ]; then
        dir="$2"
    else
        file="${2}"
        mkdirs "${dir}"
        dir=""
    fi
fi   

# construct image link: add 'echo "${google_image}"'
# after this line for debug output
google_image="${dir}/${file}"

# construct name, append number if file exists
if [[ -e "${google_image}${ext}" ]] ; then
    i=0
    while [[ -e "${google_image}(${i})${ext}" ]] ; do
        ((i++))
    done
    google_image="${google_image}(${i})${ext}"
else
    google_image="${google_image}${ext}"
fi

# get actual picture and store in google_image.$ext
wget --max-redirect 0 -qO "${google_image}" "${imagelink}"

# if 'o' flag supplied: open image
[[ $option = "o" ]] && gnome-open "${google_image}"

# successful execution, exit code 0
exit 0

评论应该是不言自明的,如果您对代码有任何疑问(例如长管道),我很乐意澄清机制。请注意,我必须在 wget 上设置更详细的用户代理,您可能需要设置不同的用户代理,但我认为这不会成为问题。如果您确实有问题,请访问 http://whatsmyuseragent.com/并在 useragent 变量中提供输出。

当您希望打开图像而不是仅下载图像时,请使用 -o 标志,如下例所示。如果您希望扩展脚本并包含自定义输出文件名,请告诉我,我会为您添加。

示例用法:

$ chmod +x getimg.sh
$ ./getimg.sh 1 dog
$ gnome-open google_image.jpg
$ ./getimg.sh -o 10 donkey

关于linux - 使用命令行从谷歌下载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27909521/

相关文章:

c++ - 为什么 `wstring_convert` 抛出 range_error?

python - 如何为 python CGIHTTPServer 创建一个 linux 服务?

shell - 将矩阵中的每两列打印在一起

PHP 转义 shell arg 不需要的行为

mysql - 复杂的网络应用多线程测试(不加载)

python - Django 模板不存在 : music/index. html

linux - 移动数据终端条码扫描仪如何集成到系统中?

linux - 虚拟机能否像基于硬件的操作系统一样高效?

linux - 如何从 shell 脚本内部更新 shell 脚本

javascript - 在 serviceWorker 之外使用 cacheStorage 不好吗?为什么?