2014-09-24 118 views
1

因此,我使用硒從網站下載文件,我發出了等待命令35秒(只需要大約10次下載),在文件夾中它放置在哪裏顯示一切正確,但我傳遞文件的名稱作爲我的程序中的參數,我總是得到最後的.part,即使該文件已完全下載並顯示在我的下載文件夾上正確。這裏是我的代碼獲取.part evertime我使用python下載selenium webdriver中的文件

Binary= FirefoxBinary('/home/what/Desktop/firefox/firefox-bin') 
profile = webdriver.FirefoxProfile() 
profile.set_preference("browser.download.folderList", 2) 
profile.set_preference("browser.download.manager.showWhenStarting", False) 
profile.set_preference("browser.download.dir", '/home/jerad/Desktop/Build') 
profile.set_preference("browser.helperApps.neverAsk.openFile", "application/octet- 
stream") 
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip") 

driver = webdriver.Firefox(firefox_profile=profile, firefox_binary=Binary) 
driver.get("website") 
driver.find_element_by_xpath("//a[contains(text(), 'DEVcrt.sp1')]").click() 

working = "/home/what/Desktop/Build" 
abspath = os.path.join(os.getcwd(), working) 
for file1 in os.listdir(abspath): 
    abspath = os.path.join(working, file1) 
    os.path.isfile(abspath) 
time.sleep(35) 
print "fnshed downloading" 
return abspath 

這裏是一個被調用它(的一部分)

j = GetUpdate() 
u = jerad.Update() 

回答

1

火狐刪除。第三部分的文件,並使該文件夾中完全下載的文件在年底類下載成功。我認爲你的代碼的問題在於你在之後調用sleep()你掃描文件的目錄。

嘗試重新安排這樣的代碼:

... 

time.sleep(35) 
print "fnshed downloading" 
for file1 in os.listdir(abspath): 
    abspath = os.path.join(working, file1) 
    if os.path.isfile(abspath): 
     break 
return abspath 

或者,你可以有Python的調查目錄每秒,並儘快恢復,因爲它發現一個非。部分文件:

... 
max_polls = 35 
polls = 0 

while polls < max_polls: 
    for file1 in os.listdir(abspath): 
     if not file1.endswith('.part') and os.path.isfile(file1): 
      print 'finished downloading' 
      return os.path.join(working, file1) 
    time.sleep(1) 
    polls += 1 

它會在完成下載後立即退出,這聽起來像只需要10秒鐘。

相關問題