By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,761 Members | 1,014 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,761 IT Pros & Developers. It's quick & easy.

How to Download a linked file on a webpage via Python

P: 1
I am trying to use Python 2.7 to open a web browser and download and save a linked kml file.

The website is:
rmgsc.cr.usgs.gov/outgoing/GeoMAC/ActiveFirePerimeters.kml

When I run the code below, a new kml file appears to be saved on my drive, however it does not bring over the entire file. Instead of 154MB, it brings over a file that is only 17 bytes.

I can either go to the website (rmgsc.cr.usgs.gov/outgoing/GeoMAC/) and then click on a link named "ActiveFirePerimeters.kml", or go to the website with the kml file name included which automatically initiates a download. In IE9 I would then need to click a "Save" button, however in Chrome it appears to automatically download. However in both only 17bytes come across....

Here is my code for Internet Explorer:

Expand|Select|Wrap|Line Numbers
  1. import os, webbrowser
  2. target_dir = r"C:\DataTest"
  3. filename = "ActiveFirePerimeters.kml"
  4.  
  5. fullname = os.path.join(target_dir,filename)
  6. with open(fullname,"w") as f:
  7.    f.write("<html>....</html>")
  8.  
  9. url = "rmgsc.cr.usgs.gov/outgoing/GeoMAC/ActiveFirePerimeters.kml"
  10. webbrowser.open(url,True,True)
  11.  
  12. os.system("taskkill /F /IM iexplore.exe")
Aug 6 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.