python - how to download pics to a specific folder location on windows? -
i have script download images given web url address:
from selenium import webdriver import urllib class chromefoxtest: def __init__(self,url): self.url=url self.uri = [] def chrometest(self): # file_name = "c:\users\administrator\downloads\images" self.driver=webdriver.chrome() self.driver.get(self.url) self.r=self.driver.find_elements_by_tag_name('img') # output=open(file_name,'w') i, v in enumerate(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, src[pos:]) # output.write(src) # output.close() if __name__=='__main__': ft=chromefoxtest("http://imgur.com/") ft.chrometest()
my question is: how make script save pics specific folder location on windows machine?
you need specify path want save file. explained in the documentation urllib.urlretrieve
:
the method is: urllib.urlretrieve(url[, filename[, reporthook[, data]]])
. , documentation says:
the second argument, if present, specifies file location copy (if absent, location tempfile generated name).
so...
urllib.urlretrieve(src, 'location/on/my/system/foo.png')
will save image specified folder.
also, consider reviewing documentation os.path
. functions manipulate file names , paths.
Comments
Post a Comment