Selenium get images
Selenium,a web automation framework, can be used to get the all of the image links from a webpage.In this article we'll given an example of that.
Get image links At first we import the selenium module and start the web driver object.
driver = webdriver.Chrome(chrome_options=options)
The webpage consists of html code, defined by tags. To show an image a webpage has a code:
<img src=".../image.jpg">
Then we find all the elements using the img tag (from html):
images = driver.find_elements_by_tag_name('img')
Finally we print the link of each image using:
for image in images:
print(image.get_attribute('src'))
Full code:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument("--test-type")
options.binary_location = "/usr/bin/chromium"
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://imgur.com/')
images = driver.find_elements_by_tag_name('img')
for image in images:
print(image.get_attribute('src'))
driver.close()
That will return all the image urls on the webpage. To download import urllib and use the line:
urllib.urlretrieve(src, "filename.png")
You could also use wget, by using os.system();