繁体   English   中英

下载并检查文件是否存在

[英]Download and check if file exists

我正在尝试获得类似的脚本。 视频
不知道他是如何做到这一点的,在每个浏览器中工作。
我不确定如何获取和检查 url 文件名。

1 - open a text file containing a list with URLs (example.com/file.exe, anotherurl.com/file2.exe)
2 - for every url, open a browser tab, download the file and check if file exists 
3 - Print "file downloaded" else "download failed" or calculate the fail ratio

我的代码,当我知道文件名时,它适用于单个 url。 我一直在努力使它适用于 url 列表。 它应该在 url 路径 /file.exe 中获取文件名

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
import os.path
import requests
op = webdriver.ChromeOptions()
p = {'download.default_directory': 'C:\\Users\\VM\\Downloads'}
op.add_experimental_option('prefs', p)
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe",
options=op)
with open("C:\\Users\\VM\\Downloads\\urls.txt",'r') as file:
    for url in file.readlines():

         driver.get(url);
         time.sleep(5)
         if os.path.isfile('CHECK URL PATH FILENAME'):
            print("File download is completed")
         else:
            print("File download is not completed") 

您可以做的是每次在下载文件之前清理文件夹(删除所有文件),然后在点击 url 检查是否存在至少一个文件(如果文件存在)您可以检查文件名是否包含“.exe”

     // clean folder by deleting all files from download location folder
     driver.get(url);
    
     time.sleep(5)
     if os.path.isfile('CHECK URL PATH FILENAME'): // get list of files present in folder ane verify if file name ends with .exe
        print("File download is completed") // Write code to delete all files after this line.
     else:
        print("File download is not

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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