Vous êtes sur la page 1sur 4

sign up log in tour help stack overflow careers search

Questions Tags Users Badges Ask Question

Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up

download images from google with command line


asked 11 months ago
I would like to download n-th image that google gives me with command line i.e. like with command
wget viewed 1359 times
1 active 6 months ago
To search image of [something] I just go to page https://www.google.cz/search?q=
[something]&tbm=isch but how do I get url of n-th search result so I can use wget?

linux shell web


Related
1 1769 Calling an external
share improve this question edited Jan 12 at 20:06 asked Jan 12 at 19:41 command in Python
398 Shell command to sum
tom integers, one per line?
234 1 17 263 Shell command to tar
directory excluding certain
files/folders

1 Answer active oldest votes 387 Exclude directory from find .


command
384 How to set a BASH variable
equal to the output from a
First attempt command?

4 First you need to set the user agent so google will authorize output from searches. Then we can look 0 Is it possible to show
for images and select the desired one. To accomplish that we insert missing newlines, wget will return interactive shell command
output on a webpage
google searches on one single line, and filter the link. The index of the file is stored in the variable without refreshing the
count . webpage?

$ count=10 10 Parallel download using Curl


command line
$ imagelink=$(wget --user-agent 'Mozilla/5.0' -qO - "www.google.be/search?q=something\&tbm=isch" | sed 's/</\n</g' utility'<img' | head
| grep
$ wget $imagelink
13 Download first 1000 images
from google search
The image will now be in your working directory, you can tweak the last command and specify a 1 Looping through files in
desired output file name. different directory given
command line argument
You can summarize it in a shell script: 1 log every linux command
line ever used?
#! /bin/bash
count=${1} Hot Network Questions
shift
query="$@" Why do we pay to present at
[ -z $query ] && exit 1 # insufficient arguments conferences?
imagelink=$(wget --user-agent 'Mozilla/5.0' -qO - | "www.google.be/search?q=${query}\&tbm=isch" | sed 's/</\n</g' | grep '<img' | head -
wget -qO google_image $imagelink How to escape "." with sed or grep?
When I plug an Ethernet cable into
my computer, is my computer
Example usage: directly connected to every server on
the Internet?
$ ls I'm in your .zips crackin' your
Documents passwords
Downloads
Music Imbuing a six year old with a sense
script.sh of mathematical wonder
$ chmod +x script.sh How to tell a foreign professor
$ bash script.sh 5 awesome politely and impressively that I cant
$ ls cover the costs of living for my PhD?
Documents
Downloads Why is 0 < -0x80000000?
google_image
What is an intuitive definition for
Music "conjugate" in Group Theory?
script.sh
Prove that the sum of two numbers
Now the google_image should contain the fifth google image when looking for 'awesome'. If you is irrational
experience any bugs, let me know, I'll take care of them. Detexify-like service where I can
paste a symbol
Better code

converted by W eb2PDFConvert.com
Better code Do I need to apologize in my
rejection email for not picking up a
The problem with this code is that it returns pictures in low resolution. A better solution is as follows: phone call from an employer I
applied to?
#! /bin/bash What is a state in physics?
# function to create all dirs til file can be made Is there a visual equivalent of the
function mkdirs { word "overhear"?
file="$1"
dir="/" How was Palpatine able to rise
through the Senate so quickly to
# convert to full path become chancellor?
if [ "${file##/*}" ]; then How to make dining alone less
file="${PWD}/${file}" awkward?
fi
"He is 80 years young"
# dir name of following dir One word for "put something to a
next="${file#/}" lower form/level/grade"
# while not filename Passive construction "have been
while [ "${next//[^\/]/}" ]; do explained": is it okay to say ""I have
# create dir if doesn't exist been explained this problem
[ -d "${dir}" ] || mkdir "${dir}" several times"?
dir="${dir}/${next%%/*}" Cos' un "babaccetto"?
next="${next#*/}"
done Constant Pressure from Advisor -
How to Stay Motivated?
# last directory to make
How would a resourceful
[ -d "${dir}" ] || mkdir "${dir}" government block Tor?
}
What's more efficient: lightened
# get optional 'o' flag, this will open the image after download armor or pocketed armor?
getopts 'o' option
Some basic questions regarding
[[ $option = 'o' ]] && shift rank-1 matrices,
# parse arguments Find the nth Fibohexaprime
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'
The comments should be self explanatory, if you have any questions about the code (such as the long
pipeline) I'll begoogle
# construct happy to clarify the mechanics. Note that I had to set a more detailed user agent on the
link
link="www.google.cz/search?q=${query}\&tbm=isch"
wget, it may happen that you need to set a different user agent but I don't think it'll be a problem. If you
do have a problem, visit http://whatsmyuseragent.com/ and supply the output in the useragent
# fetch link for download
variable.
imagelink=$(wget -e robots=off --user-agent "$useragent" -qO - "$link" | sed 's/</\n</g' | grep '<a href.*\(png\|jpg\|jpeg\)' | sed 's/.*img
imagelink="${imagelink%\%*}"
When you wish to open the image instead of only downloading, use the -o flag, example below. If you
wish
# getto file
extend the script(.png,
extention and also include
.jpg, a custom output file name, just let me know and I'll add it for
.jpeg)
you.
ext=$(echo $imagelink | sed "s/.*\(\.[^\.]*\)$/\1/")

# set default
Example usage:save location and file name change this!!
dir="$PWD"
file="google image"
$ chmod +x getimg.sh
$ ./getimg.sh 1 dog
#$ get optionalgoogle_image.jpg
gnome-open second argument, which defines the file name or dir
if [[ $# -eq 2-o]];10then
$ ./getimg.sh donkey
if [ -d "$2" ]; then
dir="$2"
shareelse
improve this answer edited May 30 at 3:34 answered Jan 12 at 20:18
file="${2}"
mkdirs "${dir}"
dir="" ShellFish
fi 3,026 1 8 24
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}"

converted by W eb2PDFConvert.com
1 @tom My pleasure, check out the script too! ShellFish Jan 12 at 20:29
1 I'll look into it. ShellFish Jan 12 at 20:34
1 I'm afraid google doesn't supply a link to the original image in their website code. It must be javascript that
handle's linking to the original image. If you can find it in google's code (using a browser's "inspect element")
I'll be happy to take care of it. ShellFish Jan 12 at 20:50
1 I was mistaken, I will look into it. ShellFish Jan 12 at 21:14
1 The script should now take a second argument, which is either a target dir (where it will save the image as
"google image.ext" or "google image(1).ext" etc if the file already exists. You can also supply a file name as
second argument such as /home/user/Documents/images/image . The extension will be added
automatically. -- Let me kniw if there are bugs. ShellFish May 28 at 23:28

show 21 more comments

Your Answer

Sign up or log in Post as a guest


Sign up using Google Name

Sign up using Facebook


Email

Sign up using Email and Password required, but never shown

Post Your Answer

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged linux shell web or ask your own
question.

question feed

tour help blog chat data legal privacy policy work here advertising info mobile contact us feedback

TECHNOLOGY LIFE / ARTS CULTURE / SCIENCE OTHER


RECREATION

converted by W eb2PDFConvert.com
Stack Overflow Programmers Database Photography English Language & Mathematics Stack Apps
Administrators Usage
Server Fault Unix & Linux Science Fiction & Cross Validated (stats) Meta Stack Exchange
Drupal Answers Fantasy Skeptics
Super User Ask Different (Apple) Theoretical Computer Area 51
SharePoint Graphic Design Mi Yodeya (Judaism) Science
Web Applications WordPress Stack Overflow
Development User Experience Movies & TV Travel Physics Careers
Ask Ubuntu
Geographic Mathematica Seasoned Advice Christianity MathOverflow
Webmasters Information Systems (cooking)
Salesforce Arqade (gaming) Chemistry
Game Development Electrical Engineering Home Improvement
ExpressionEngine Bicycles Biology
TeX - LaTeX Android Enthusiasts Answers Personal Finance &
Money Role-playing Games more (5)
Information Security more (13)
Academia more (21)
more (9)

site design / logo 2015 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required
rev 2015.12.11.3072

converted by W eb2PDFConvert.com

Vous aimerez peut-être aussi