如何使用selenium下载文件?

时间:2013-09-23 08:21:29

标签: python python-2.7 selenium selenium-webdriver beautifulsoup

我正在尝试获取下载链接并下载文件。

我有一个包含以下链接的日志文件:

http://www.downloadcrew.com/article/18631-aida64
http://www.downloadcrew.com/article/4475-sumo
http://www.downloadcrew.com/article/2174-iolo_system_mechanic_professional
...
...

我有这样的代码:

import urllib, time

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

f = open("dcrewtest.txt")

for line in f.readlines():
    try:
        driver.find_element_by_xpath("//div/div[2]/div[2]/div[2]/div[3]/div/a/img").click()
        time.sleep(8)
    except:
        pass 

     url = line.encode
     pageurl = urllib.urlopen(url).read()
     soup = BeautifulSoup(pageurl)
     for a in soup.select("h1#articleTitle"):
         print a.contents[0].strip()

     for b in soup.findAll("th"):
         if b.text == "Date Updated:":
            print b.parent.td.text
         elif b.text == "Developer:":
            print c.parent.td.text

到此为止我不知道如何获取下载链接并下载它。 是否可以使用selenium下载文件?

1 个答案:

答案 0 :(得分:4)

根据documentation,您应配置FirefoxProfile以自动下载具有指定内容类型的文件。以下是使用txt文件中第一个将exe文件保存在当前目录中的URL的示例:

import os
from selenium import webdriver


fp = webdriver.FirefoxProfile()

fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir", os.getcwd())
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-msdos-program")

driver = webdriver.Firefox(firefox_profile=fp)
driver.get("http://www.downloadcrew.com/article/18631-aida64")

driver.find_element_by_xpath("//div[@class='downloadLink']/a/img").click()

请注意,我还简化了xpath。