所以我正在使用硒从网站上下载文件,我已经发出了35秒的等待命令(下载大约只需要10秒),在放置它的文件夹中显示一切正确,但是我通过了文件作为程序中的参数,即使文件已完全下载并正确显示在我的下载文件夹中,我也总是在末尾得到.part。这是我的代码
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()
最佳答案
Firefox会删除.part文件,并在成功下载结束后将完全下载的文件保留在文件夹中。我认为您的代码存在问题,是您在扫描目录中的文件后正在调用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秒钟。