简体   繁体   中英

Python : Downloading file from the link which is implemented with php

I am trying to download file from the web page. The link of file is implemented by php : ~/download.php?id=~

The download of a file is possible to click the link or right-click and select the menu, "save this file" in the web browser.

At first, I used the selenium with phantomjs . It was successful to get the link with tag " a " by " find_element ". I performed clicking or right-clicking with ActionChains of selenium , but it couldn't download the file. By searching the web, it looks like phantomjs doesn't support the download of a file.

What I consider to use as second way is using firefox or chrome which looks like supporting downloading file. Please give me an advice whether this way is the best or not. I am running the program on raspberry pi b+ . Thank you very much.

The easiest way to download file:

import urllib
url = "http://domain.com/~/download.php?id=~"
path_to_file = "/local/folder/where/you/want/to/save/file/file_name"

Python 2.x

urllib.urlretrieve(url, path_to_file)

Python 3.x

urllib.request.urlretrieve(url, path_to_file)

If you need to download file with selenium :

Firefox

from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
profile = FirefoxProfile ()
profile.set_preference("browser.download.folderList",2)
profile.set_preference("browser.download.manager.showWhenStarting",False)
profile.set_preference("browser.download.dir", '/download/folder/by/default')
profile.set_preference("browser.helperApps.neverAsk.saveToDisk",file_MIME_type)
driver = webdriver.Firefox(firefox_profile=profile)

Chrome

from selenium import webdriver
download_dir = "/download/folder/by/default"
chrome_options = webdriver.ChromeOptions()
preferences = {"download.default_directory": download_dir ,
                      "directory_upgrade": True,
                      "safebrowsing.enabled": True }
chrome_options.add_experimental_option("prefs", preferences)
driver = webdriver.Chrome(chrome_options=chrome_options)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM