加入收藏 | 设为首页 | 会员中心 | 我要投稿 台州站长网 (https://www.0576zz.cn/)- 边缘计算、中间件、数据处理、数据分析、智能存储!
当前位置: 首页 > 综合聚焦 > Linux > 正文

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#

(编辑:台州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读