This question already has answers here:
Python download a file
(2 answers)
Closed 9 years ago.
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")
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)
Related
I am trying to write some code that would download a file. Now this file from this website specifically, once you go on to that link, it takes 5 seconds for it to actually prompt the download, for example: https://sourceforge.net/projects/esp32-s2-mini/files/latest/download
I have tried using the obvious methods, such as wget.download and urllib.request.urlretrieve
urllib.request.urlretrieve('https://sourceforge.net/projects/esp32-s2-mini/files/latest/download', 'zzz')
get.download('https://sourceforge.net/projects/esp32-s2-mini/files/latest/download', 'zzzdasdas')
However, that does not work, it downloads something else, but not what I want it to.
Any suggestions would be great.
Using chrome's download page (ctrl+j should open it, or just click "Show All" when downloading a file), we can see all of our recent downloads. The link you provided is just the page that begins the download, not the location of the actual file itself. Right-clicking the blue name lets us copy the address to the actual file being downloaded.
The actual link of the file, in this case, is https://cfhcable.dl.sourceforge.net/project/esp32-s2-mini/ToolFlasher/NodeMCU-PyFlasher-3.0-x64.exe
We can then make a GET request to download the file. Testing this out with bash wget downloads the file properly.
wget https://versaweb.dl.sourceforge.net/project/esp32-s2-mini/ToolFlasher/NodeMCU-PyFlasher-3.0-x64.exe
You can, of course, use python requests to accomplish this as well.
import requests
response = requests.get(r"https://cfhcable.dl.sourceforge.net/project/esp32-s2-mini/ToolFlasher/NodeMCU-PyFlasher-3.0-x64.exe")
with open("NodeMCU-PyFlasher-3.0-x64.exe", "wb") as f:
f.write(response.content)
Note that we are using wb (write bytes) mode instead of the default w (write).
I am developing a plugin for PyCharm in Java.I use Pyan3 to generate a static graph. Pyan3 gives me an HTML file as a result, which I want to display.
I tried with the webbrowser but failed.
import webbrowser
import os
webbrowser.open('file://' + os.path.realpath('myuses2.html'))
Is it possible to open the html file inside pyCharm? (from code line)
(If it is possible to open, than there is any way to give back information from the HTML? I want the code to jump to a function name when the user clicks on it.)
After some research on my problem, it seems I should use either requests or urllib or both.
So basically, I am trying to learn the code I need to download a csv file from this url:
https://globalaccess.sustainalytics.com/#/tools/0
The way I manually download my files is as follows: first, I need to log in using username and password. Next I have to go to a tab called "Screening" that takes to me another page that has several buttons called "Generate". I click a specific generate button (it's always the same one) among the option to get the excel file. After that I have the option to save the file or open from a little window within the website.
My question is what code can I use on Python to download and save the file in a particular folder?
Use Selenium
https://selenium-python.readthedocs.io/
You'll need to download a 'chromedriver' to the same directory as your python script, then use the intro tutorial on the selenium docs site to drive the browser to type/click where you want.
If you use chrome you can right click on any given link/input box click inspect, then in the window that comes up right click the bit of highlighted code and 'copy xpath'. Use the find element by xpath function in Selenium to send keys or clicks to that element.
This question already has an answer here:
How to use urllib to download image from web
(1 answer)
Closed 7 years ago.
I use the following code to download the image 14112758275517_800X533.jpg.
The problem is that I cannot open the 14112758275517_800X533.jpg saved as G:\\image.jpg because the
Windows photo viewer was unable to open the picture, as the file may be corrupted, damaged or too large
import urllib
imageurl="http://img.vogue.com.cn/userfiles/201409/14112758275517_800X533.jpg"
pic_name = "G:\\image.jpg"
urllib.urlretrieve(imageurl, pic_name)
How can I download the image so that it is readable?
I think you cannot. It is likely a website problem, not something in the code you posted.
I am given a 403 Forbidden when trying to type the url you gave even with a navigator.
As an example, the following works and only the url has changed :
import urllib
imageurl="https://www.python.org/static/img/python-logo.png"
pic_name = "./image.png"
urllib.urlretrieve(imageurl, pic_name)
However, you might want to check other topics about the subjet as there are more advanced techniques to download images from the web such as https://stackoverflow.com/a/8389368/2549230
This question already has answers here:
python's webbrowser launches IE, instead of default browser, on Windows relative path
(14 answers)
Closed 8 years ago.
I want to google search a specific phrase using python program that too in a specific browser like Mozilla/google chrome.
import webbrowser
url = "https://www.google.com.tr/search?q={}".format("Raspberry Pi")
webbrowser.open(url)
I read this script in some post but it always go for the default browser in the system, can't i select or specify a different browser.
Use webbrowser.get('browser') to specify a browser.
import webbrowser
url = "https://www.google.com.tr/search?q={}".format("Raspberry Pi")
b = webbrowser.get('lynx')
b.open(url)