Python – How to download all images from a web page using Python?

How to download all images from a web page using Python?… here is a solution to the problem.

How to download all images from a web page using Python?

I have this script

that opens a browser page with the desired web page I want to download all images from this page, how do I do given this script :

from selenium import webdriver
import urllib
class ChromefoxTest:
    def __init__(self,url):
        self.url=url
    def chromeTest(self):
        self.driver=webdriver. Chrome()
        self.driver.get(self.url)
        self.r=self.driver.find_element_by_tag_name('img')
        self.uri=self.r.get_attribute("src")
        self.g=urllib.urlopen(self.uri)
if __name__=='__main__':
    FT=ChromefoxTest("http://www.google.com")
    FT.chromeTest()

Solution

from selenium import webdriver
import urllib

class ChromefoxTest:
    def __init__(self,url):
        self.url=url
        self.uri = []
        self.folder = '/home/palladin/imgs'

def chromeTest(self):
        self.driver=webdriver. Chrome()
        self.driver.get(self.url)
        self.r=self.driver.find_elements_by_tag_name('img')
        for v in self.r:
            src = v.get_attribute("src")
            self.uri.append(src)
            pos = len(src) - src[::-1].index('/')
            print src[pos:]
            self.g=urllib.urlretrieve(src, "/".join([self.folder, src[pos:]]))

if __name__=='__main__':
    FT=ChromefoxTest("http://www.google.com")
    FT.chromeTest()

Related Problems and Solutions