I wrote a script for image scraping on google images using selenium webdriver. The webdriver navigates through images and fetches the url. But, today when I ran the script, it didn't get the url for any of the images.
from selenium import webdriver
import urllib.request
from PIL import Image
import os
keyword=input('keyword : ')
n=150
url=input('url : ')
# provide path to dircetory before running the code
path='E://Old//cust_data'
if keyword not in os.listdir(path):
os.mkdir(path+'//'+keyword)
img_dir=path+'//'+keyword
driver=webdriver.Chrome('E://Old//card//chromedriver.exe')
driver.get(url)
i=1
j=1
while j<=n:
try:
driver.find_element_by_xpath('//*[@id="islrg"]/div[1]/div[{}]/a[1]/div[1]/img'.format(i)).click()
img=driver.find_element_by_xpath('//*[@id="Sva75c"]/div/div/div[3]/div[2]/c-wiz/div/div[1]/div[1]/div/div[2]/a/img')
link=img.get_attribute('src')
print(link)
urllib.request.urlretrieve(link,img_dir+'//'+keyword+' '+str(j)+'.jpg')
size=os.stat(img_dir+'//'+keyword+' '+str(j)+'.jpg').st_size
if size<15000:
os.remove(img_dir+'//'+keyword+' '+str(j)+'.jpg')
else:
im=Image.open(img_dir+'//'+keyword+' '+str(j)+'.jpg')
print(keyword+' '+str(j)+'.jpg',(im.size[0],im.size[1]))
j+=1
i+=1
except:
i+=1
print('error')
pass
driver.close()
Its returns 'error' for every image. It was working fine, I don't know what's causing this. Also, while navigation, the webdriver sometimes just stops. There's no error or anything, it just stops.