linux – 从命令行下载图像
发布时间:2021-02-21 04:01:12 所属栏目:Linux 来源:互联网
导读:我想下载第n个图像,谷歌给我的命令行,就像命令wget 要搜索[某事]的图像,我只需转到页面https://www.google.cz/search?q=[something]u0026amp;tbm=isch,但如何获取第n个搜索结果的网址,以便我可以使用wget的? 第一次尝试 首先,您需要设置用户代理,所以Google
我想下载第n个图像,谷歌给我的命令行,就像命令wget 要搜索[某事]的图像,我只需转到页面https://www.google.cz/search?q=[something]u0026amp;tbm=isch,但如何获取第n个搜索结果的网址,以便我可以使用wget的? 解决方法第一次尝试首先,您需要设置用户代理,所以Google将授权搜索的输出.然后我们可以查找图像并选择所需的图像.为了完成这一点,我们插入缺少的换行符,wget将在一行中返回google搜索,并过滤链接.文件的索引存储在变量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’.如果您遇到任何错误,请告诉我,我会照顾他们. 更好的代码 该代码的问题是它以低分辨率返回图片.一个更好的解决方案如下: #! /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# |