Reputation: 157
Hi I'm learning how to code & script with python
I have just finished writing a script that finds all images on a webpage and prints the links onto the screen, I'm now trying to get it to download the images that where printed onto the screen to a temp folder within the C: drive (C:\temp)
.
I'm not sure how to do that though and after trying to search the web i couldn't get anything to work. How can i download the images that i found without changing my code to much?
Upvotes: 0
Views: 244
Reputation: 46593
urllib.urlretrieve
for Python 2.7:
import urllib
urllib.urlretrieve(url, absolute_path_for_downloaded_file)
urllib.request.urlretrieve
for Python 3:
import urllib.request
urllib.request.urlretrieve(url, absolute_path_for_downloaded_file)
Make the following modifications to your code:
Somewhere at the top of the script:
import os.path # [+] Added line
import urllib # [+] Added line
Inside the getImage
function:
print '[+]', str(len(images)), 'Images Found:'
for img in images:
print img
return images # [+] Added line
Inside the main
function:
# Get the web page
page = webpage.wget(sys.argv[1])
# Get the links
for x in getImage(page): # [+] Modified line
urllib.urlretrieve(x, os.path.join('C:\\temp', x.split('/')[-1])) # [+] Modified line
Upvotes: 2