from bs4 import BeautifulSoup from urllib.request import urlopen import re import urllib.request import os def get_soup(url,header): return BeautifulSoup(urlopen(urllib.request.Request(url,headers=header)),"html.parser") # you can change the query for the image here query = "dog" query= query.split() query='+'.join(query) url="https://www.google.co.in/search?q="+query+"&source=lnms&tbm=isch" header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'} soup = get_soup(url,header) images = [a['src'] for a in soup.find_all("img", {"src": re.compile("gstatic.com")})] #change the file name here image_type = "Action" #print images for img in images: raw_img = urlopen(img).read() #add the directory for your image here DIR="C:\\Users\\allewellyn\\Desktop\\Picture\\" cntr = len([i for i in os.listdir(DIR) if image_type in i]) + 1 print(cntr) f = open(DIR + image_type + "_"+ str(cntr)+".jpg", 'wb') f.write(raw_img) f.close()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question