Loading an Image Using Python Mechanize

I am trying to write a Python script to download an image and set it as wallpaper. Unfortunately, the Mechanize documentation is pretty poor. My script works correctly by reference, but it's hard for me to save image on my computer. From what I explored, the .retrieve () method should do the job, but how do I specify the path to where the file should be uploaded? Here is what I have ...

def followLink(browser, fixedLink): browser.open(fixedLink) if browser.find_link(url_regex = r'1600x1200'): browser.follow_link(url_regex = r'1600x1200') elif browser.find_link(url_regex = r'1400x1050'): browser.follow_link(url_regex = r'1400x1050') elif browser.find_link(url_regex = r'1280x960'): browser.follow_link(url_regex = r'1280x960') return 
+4
source share
4 answers
 import mechanize, os from BeautifulSoup import BeautifulSoup browser = mechanize.Browser() html = browser.open(url) soup = BeautifulSoup(html) image_tags = soup.findAll('img') for image in image_tags: filename = image['src'].lstrip('http://') filename = os.path.join(dir, filename.replace('/', '_')) data = browser.open(image['src']).read() browser.back() save = open(filename, 'wb') save.write(data) save.close() 

This will help you download all the images from the web page. As for html parsing, you'd better use BeautifulSoup or lxml. And the download just reads the data and then writes it to a local file. You must assign your value to the directory. This is where you see the images.

+9
source

I don’t know why this solution did not appear, but you can also use the mechanize.Browser.retrieve function. Perhaps this only works in newer versions of mechanize and is therefore not mentioned?

Anyway, if you want to shorten zhangyangyu's answer , you can do this:

 import mechanize, os from BeautifulSoup import BeautifulSoup browser = mechanize.Browser() html = browser.open(url) soup = BeautifulSoup(html) image_tags = soup.findAll('img') for image in image_tags: filename = image['src'].lstrip('http://') filename = os.path.join(dir, filename.replace('/', '_')) browser.retrieve(image['src'], filename) browser.back() 

Also keep in mind that you probably want to put all this in a try except block like this:

 import mechanize, os from BeautifulSoup import BeautifulSoup browser = mechanize.Browser() html = browser.open(url) soup = BeautifulSoup(html) image_tags = soup.findAll('img') for image in image_tags: filename = image['src'].lstrip('http://') filename = os.path.join(dir, filename.replace('/', '_')) try: browser.retrieve(image['src'], filename) browser.back() except (mechanize.HTTPError,mechanize.URLError) as e: pass # Use e.code and e.read() with HTTPError # Use e.reason.args with URLError 

Of course, you will want to customize this to your needs. Perhaps you want it to work if it encounters a problem. It completely depends on what you want to achieve.

+5
source

You can upload / download image by opening img src url.

 image_response = browser.open_novisit(img['src']) 

to save the file now just use fopen:

 with open('image_out.png', 'wb') as f: f.write(image_response.read()) 
+3
source

It's really crap, but it "works" for me, 0xc0000022l anwer

import mechanize, os from BeautifulSoup import BeautifulSoup import urllib2

 def DownloadIMGs(url): # IMPORTANT URL WITH HTTP OR HTTPS print "From", url dir = 'F:\Downloadss' #Dir for Downloads basicImgFileTypes = ['png','bmp','cur','ico','gif','jpg','jpeg','psd','raw','tif'] browser = mechanize.Browser() html = browser.open(url) soup = BeautifulSoup(html) image_tags = soup.findAll('img') print "N Images:", len(image_tags) print #---------SAVE PATH #check if available if not os.path.exists(dir): os.makedirs(dir) #---------SAVE PATH for image in image_tags: #---------SAVE PATH + FILENAME (Where It is downloading) filename = image['src'] fileExt = filename.split('.')[-1] fileExt = fileExt[0:3] if (fileExt in basicImgFileTypes): print 'File Extension:', fileExt filename = filename.replace('?', '_') filename = os.path.join(dir, filename.split('/')[-1]) num = filename.find(fileExt) + len(fileExt) filename = filename[:num] else: filename = filename.replace('?', '_') filename = os.path.join(dir, filename.split('/')[-1]) + '.' + basicImgFileTypes[0] print 'File Saving:', filename #---------SAVE PATH + FILENAME (Where It is downloading) #--------- FULL URL PATH OF THE IMG imageUrl = image['src'] print 'IMAGE SRC:', imageUrl if (imageUrl.find('http://') > -1 or imageUrl.find('https://') > -1): pass else: if (url.find('http://') > -1): imageUrl = url[:len('http://')] imageUrl = 'http://' + imageUrl.split('/')[0] + image['src'] elif(url.find('https://') > -1): imageUrl = url[:len('https://')] imageUrl = 'https://' + imageUrl.split('/')[0] + image['src'] else: imageUrl = image['src'] print 'IMAGE URL:', imageUrl #--------- FULL URL PATH OF THE IMG #--------- TRY DOWNLOAD try: browser.retrieve(imageUrl, filename) print "Downloaded:", image['src'].split('/')[-1] print except (mechanize.HTTPError,mechanize.URLError) as e: print "Can't Download:", image['src'].split('/')[-1] print pass #--------- TRY DOWNLOAD browser.close() DownloadIMGs('https://stackoverflow.com/questions/15593925/downloading-a-image-using-python-mechanize') 
0
source

All Articles