我想下载谷歌给我的命令行的第n个图像,即与命令一样 wget
要搜索[something]
我的图像,只需转到页面,https://www.google.cz/search?q=[something]&tbm=isch
但如何获取第n个搜索结果的网址,以便我可以使用wget?
She*_*ish 19
第一次尝试
首先,您需要设置用户代理,以便谷歌授权搜索输出.然后我们可以查找图像并选择所需的图像.为了实现这一点,我们插入缺少的换行符,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
Run Code Online (Sandbox Code Playgroud)
图像现在将在您的工作目录中,您可以调整最后一个命令并指定所需的输出文件名.
您可以在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
Run Code Online (Sandbox Code Playgroud)
用法示例:
$ ls
Documents
Downloads
Music
script.sh
$ chmod +x script.sh
$ bash script.sh 5 awesome
$ ls
Documents
Downloads
google_image
Music
script.sh
Run Code Online (Sandbox Code Playgroud)
现在google_image
应该包含第五个谷歌图像时寻找'真棒'.如果您遇到任何错误,请告诉我,我会照顾他们.
更好的代码
此代码的问题是它以低分辨率返回图片.更好的解决方案如下:
#! /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
Run Code Online (Sandbox Code Playgroud)
评论应该是自我解释的,如果您对代码有任何疑问(例如长管道),我将很乐意澄清这些机制.请注意,我必须在wget上设置更详细的用户代理,可能需要设置不同的用户代理,但我认为这不是问题.如果确实有问题,请访问 http://whatsmyuseragent.com/并在useragent
变量中提供输出.
如果您想打开图像而不是仅下载图像,请使用-o
下面的示例标记.如果您希望扩展脚本并包含自定义输出文件名,请告诉我,我会为您添加它.
用法示例:
$ chmod +x getimg.sh
$ ./getimg.sh 1 dog
$ gnome-open google_image.jpg
$ ./getimg.sh -o 10 donkey
Run Code Online (Sandbox Code Playgroud)
这是ShellFish提供的答案的补充.为了解决这个问题,他们非常尊重他们.:)
谷歌最近改变了他们的图像结果页面的网页代码,不幸的是,这个页面破坏了贝类的代码.我每天晚上都在一个cron工作中使用它,直到大约4天前它停止接收搜索结果.在研究这个问题时,我发现谷歌已经删除了像imgurl这样的元素,并且已经将更多内容转移到了javascript中.
我的解决方案是扩展了贝类的优秀代码,但已对其进行了修改以处理这些Google更改,并包含了我自己的一些"增强功能".
它执行单个Google搜索,保存结果,批量下载指定数量的图像,然后使用ImageMagick将这些图像构建到单个图库图像中.最多可以请求1,000张图像.
这个bash脚本可以在https://git.io/googliser上找到
谢谢.