2

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:

import os, webbrowser
target_dir = r"C:\DataTest"
filename = "ActiveFirePerimeters.kml"

fullname = os.path.join(target_dir,filename)
with open(fullname,"w") as f:
   f.write("<html>....</html>")

url = "rmgsc.cr.usgs.gov/outgoing/GeoMAC/ActiveFirePerimeters.kml"
webbrowser.open(url,True,True)

os.system("taskkill /F /IM iexplore.exe")
5
  • 1
    Why are you opening the file in a web browser? Can you not directly download the file?
    – Nadh
    Aug 6, 2013 at 16:34
  • 2
    see stackoverflow.com/questions/9899031/python-download-a-file?rq=1
    – user180100
    Aug 6, 2013 at 16:34
  • If I hit this URL FF reports file size 1,7Mb. Maybe a server fault ...
    – PeterMmm
    Aug 6, 2013 at 16:35
  • @Nadh - I only need to download the latest version of the file. The file is updated on a regular basis. I want to grab the file and download it to my computer then run a python script on it, etc. If I don't have to open a browser, then that would be better. Aug 6, 2013 at 17:38
  • @PeterMmm - The file is updated regularly so the size will change throughout the day. How can I download it to a specific file on my computer via Python 2.7? I'm not a programmer so I need an example of a working code instead of a generic piece of code. Aug 6, 2013 at 18:02

1 Answer 1

1

Are you required to use Internet Explorer for some reason to accomplish this task ? I would suggest taking a look at the urllib module : http://docs.python.org/library/urllib.html. In particular, you can download a resource easily using :

import urllib

urllib.urlretrieve(source_url, local_file_path)
3
  • @Imjohns3 - No, I don't have a specific browser. I was experiment with both to see which would work easier. Than for the code. I'll give it a try. So for clarification, how does this work? I just tried to replace the source_url with "rmgsc.cr.usgs.gov/outgoing/GeoMAC/ActiveFirePerimeters.kml" and the local_file_path with "C:\DataTest\ActiveFiresPerimeters.kml" and it did not work. Any clues? Aug 6, 2013 at 17:43
  • You probably need to include the protocol in your URL (e.g., 'http://')
    – lmjohns3
    Aug 7, 2013 at 14:08
  • Did the above solution work? I’m trying to do the exact same thing. Aug 30, 2019 at 13:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.