Search code examples
python-3.xseleniumgoogle-chromeselenium-chromedriverurllib

How do I download a image from a url with Python 3?


Can anyone help me with the below code, I'm trying just to download the image from the below url using Python. I looked online and got the below code but it giving me the below error on the urllib request?

import urllib.request 
from selenium import webdriver
driver = webdriver.Chrome('c://CSALE//chromedriver.exe')
driver.get('https://kbdevstorage1.blob.core.windows.net/asset-blobs/19950_en_1')
img = driver.find_element_by_xpath('/html/body/img').get_attribute('src')
urllib.request.urlretrieve(img, "wolf.png")
driver.close()

Error:

urllib.error.URLError: <urlopen error [WinError 10061] No connection could be made because the target machine actively refused it>

Solution

  • I did the below with request module :

    img = driver.find_element_by_xpath('/html/body/img').get_attribute('src')
    
    data = requests.get(img).content
    local_filename = 'Dog_with_Selenium.jpg'
    
    with open(local_filename, 'wb') as f:
      f.write(data)