YouTube Video Link: Download Images from a webpage using Python Selenium and Requests Library
Python Code:
import time
import requests
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.chrome.service import Service
ser = Service("C:\\chromedriver_win32\\chromedriver.exe")
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("window-size=1920,1080")
caps = DesiredCapabilities().CHROME
caps["pageLoadStrategy"] = "eager"
driver =webdriver.Chrome(service=ser,options=chrome_options,desired_capabilities=caps)
#%%
driver.get('https://pixabay.com/images/search/sun/')
#%%
img_elements=driver.find_elements(By.CSS_SELECTOR,'#content > div > div:nth-child(3) > div > div.row-masonry.search-results > div > div > div > div > a > img')
#%%
img_list_urls=[]
for x in img_elements:
q=x.get_attribute('src')
if q.find('.jpg')!=-1:
img_list_urls.append(q)
#%%
print('No. of Images '+str(len(img_list_urls)))
#%%
for url in img_list_urls:
file_name='C:/Users/Devashish/a_Blink_Coders/sun/'+url[url.rindex('/')+1:]
with open(file_name,'wb') as f:
f.write(requests.get(url).content)
#%%
Thanks for Visiting Blinks Coders!
Tags:
python, selenium,how to download images from webpage using python,python download image url requests,python requests,how to download images from website using python,python programming,python selenium,python tutorial,download all images from a website,download all images from website python,download images using python,python download all images from url,selenium with python,selenium webdriver,python selenium tutorial
Post a Comment