New to Python. I'm trying to find a way to get a url of the first picture I get from google search for some string. For example if I type "dog" I would like to get the first picture url for dog. I don't care which one just some url from Google image search.

Is it possible? what is the easiest way to do it? I saw from previous threads many ways to extract/download the image - but I just need the url and it doesn't matter which one.

1 Answers

0
Avi Thour On

This should work, simply replace the word to get images of anything. Make sure you have requests and BeautifulSoup, if not run this command:

pip install requests beautifulsoup4

import requests
from bs4 import BeautifulSoup

word = 'dog'
url = 'https://www.google.com/search?q={0}&tbm=isch'.format(word)
content = requests.get(url).content
soup = BeautifulSoup(content,'lxml')
images = soup.findAll('img')

for image in images:
    print(image.get('src'))