gpt4 book ai didi

regex - 从谷歌搜索下载前 1000 张图片

转载 作者:行者123 更新时间:2023-12-03 14:42:29 25 4
gpt4 key购买 nike

我对谷歌图片进行了一些搜索

http://www.google.com/search?hl=en&q=panda&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&biw=1287&bih=672&um=1&ie=UTF-8&tbm=isch&source=og&sa=N&tab=wi&ei=qW4FUJigJ4jWtAbToInABg

结果是成千上万的照片。我正在寻找一个可以下载第一个 n 的 shell 脚本。图像,例如 1000 或 500。

我怎样才能做到这一点 ?

我想我需要一些高级正则表达式或类似的东西。我尝试了很多东西但无济于事,有人可以帮助我吗?

最佳答案

更新 4: PhantomJS 现在已经过时了,我使用 Selenium 和 Chrome headless 在 Python 中创建了一个新脚本 google-images.py。更多详情请看这里:https://stackoverflow.com/a/61982397/218294

更新 3:我修复了脚本以使用 phantomjs 2.x。

更新 2:我修改了脚本以使用 phantomjs。它更难安装,但至少它可以再次工作。 http://sam.nipl.net/b/google-images http://sam.nipl.net/b/google-images.js

更新 1:不幸的是,这不再有效。现在似乎需要 Javascript 和其他魔法来查找图像的位置。这是雅虎图像搜索脚本的一个版本:http://sam.nipl.net/code/nipl-tools/bin/yimg

原答案:为此,我一起破解了一些东西。我通常会编写较小的工具并将它们一起使用,但您要求的是一个 shell 脚本,而不是三打。这是故意密集的代码。

http://sam.nipl.net/code/nipl-tools/bin/google-images

到目前为止,它似乎工作得很好。请让我知道您是否可以改进它,或者建议任何更好的编码技术(假设它是一个 shell 脚本)。

#!/bin/bash
[ $# = 0 ] && { prog=`basename "$0"`;
echo >&2 "usage: $prog query count parallel safe opts timeout tries agent1 agent2
e.g. : $prog ostrich
$prog nipl 100 20 on isz:l,itp:clipart 5 10"; exit 2; }
query=$1 count=${2:-20} parallel=${3:-10} safe=$4 opts=$5 timeout=${6:-10} tries=${7:-2}
agent1=${8:-Mozilla/5.0} agent2=${9:-Googlebot-Image/1.0}
query_esc=`perl -e 'use URI::Escape; print uri_escape($ARGV[0]);' "$query"`
dir=`echo "$query_esc" | sed 's/%20/-/g'`; mkdir "$dir" || exit 2; cd "$dir"
url="http://www.google.com/search?tbm=isch&safe=$safe&tbs=$opts&q=$query_esc" procs=0
echo >.URL "$url" ; for A; do echo >>.args "$A"; done
htmlsplit() { tr '\n\r \t' ' ' | sed 's/</\n</g; s/>/>\n/g; s/\n *\n/\n/g; s/^ *\n//; s/ $//;'; }
for start in `seq 0 20 $[$count-1]`; do
wget -U"$agent1" -T"$timeout" --tries="$tries" -O- "$url&start=$start" | htmlsplit
done | perl -ne 'use HTML::Entities; /^<a .*?href="(.*?)"/ and print decode_entities($1), "\n";' | grep '/imgres?' |
perl -ne 'use URI::Escape; ($img, $ref) = map { uri_unescape($_) } /imgurl=(.*?)&imgrefurl=(.*?)&/;
$ext = $img; for ($ext) { s,.*[/.],,; s/[^a-z0-9].*//i; $_ ||= "img"; }
$save = sprintf("%04d.$ext", ++$i); print join("\t", $save, $img, $ref), "\n";' |
tee -a .images.tsv |
while IFS=$'\t' read -r save img ref; do
wget -U"$agent2" -T"$timeout" --tries="$tries" --referer="$ref" -O "$save" "$img" || rm "$save" &
procs=$[$procs + 1]; [ $procs = $parallel ] && { wait; procs=0; }
done ; wait

特征:
  • 小于 1500 字节
  • 解释用法,如果在没有参数的情况下运行
  • 并行下载完整图像
  • 安全搜索选项
  • 图像大小、类型等 opts 字符串
  • 超时/重试选项
  • 冒充 googlebot 获取所有图片
  • 数字图像文件
  • 保存元数据

  • 有时间我会发布一个模块化版本,以展示它可以用一组 shell 脚本和简单的工具很好地完成。

    关于regex - 从谷歌搜索下载前 1000 张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11524218/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com