selenium 如何重命名PDF文件Whit的URL从我下载它使用Python

cyej8jka  于 2022-11-24  发布在  Python
关注(0)|答案(3)|浏览(135)

这是我对你们所有人的第一个问题,我希望你们都在做格栅。
我的问题是,我有一个链接,我已经从谷歌搜索结果收集的列表,我正在下载文件使用从链接使用(它是下载PDF文件) selenium 我想重命名每个文件对它的URL谁可以我做这个我没有尝试过任何代码,所以请帮助我。我显示的代码 selenium ,我用来下载文件。

folderName=input(("Enter The FolderName:\t")).upper() #Geting Input for the name of folder

newDir="C:\\Users\\sulta\\Data Science CV\\" + folderName
print(newDir)
if not os.path.exists(newDir):
    os.makedirs(newDir) #creating folder 
options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', {
"download.default_directory":"C:\\Users\\sulta\\Data Science CV\\" + folderName, #Downloading the files to thi path
"download.prompt_for_download": False, #To auto download the file
"download.directory_upgrade": True,
"plugins.always_open_pdf_externally": True #It will not show PDF directly in chrome
})
driver = webdriver.Chrome(options=options)
for z in range(len(link)): #My All links are stored in the list named link
    try:
        driver.get(link[z])
        driver.set_page_load_timeout(10)
    except:
        continue
vkc1a9a2

vkc1a9a21#

我不认为有任何python核心库可以在selenium下载时做到这一点。你可以做的是有一个文件夹看门狗,它可以跟踪文件夹中发生的任何更改或事件,这样你就可以从那里重命名新文件。检查pyWatch,它可能会有所帮助。

sirbozc5

sirbozc52#

一种解决方案是读取目录中的所有文件,下载新文件,然后再次读取以获取其名称,然后将其重命名为您想要的名称,如下所示

folderName=input(("Enter The FolderName:\t")).upper() #Geting Input for the name of folder

newDir="C:\\Users\\sulta\\Data Science CV\\" + folderName
print(newDir)
if not os.path.exists(newDir):
    os.makedirs(newDir) #creating folder 
options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', {
"download.default_directory":"C:\\Users\\sulta\\Data Science CV\\" + folderName, #Downloading the files to thi path
"download.prompt_for_download": False, #To auto download the file
"download.directory_upgrade": True,
"plugins.always_open_pdf_externally": True #It will not show PDF directly in chrome
})
driver = webdriver.Chrome(options=options)
for z in range(len(link)): #My All links are stored in the list named link
    try:
        import os
        files_before = os.listdir(newDir) #list all files
        driver.get(link[z])
        driver.set_page_load_timeout(10)
        files_after = os.listdir(newDir) # list all files after download

        new_file = list(set(files_after) - set(files_before))[0] # get new file in folder
        new_name = 'new_name.file' #name of new file
        os.rename(newDir+'/'+new_file,newDir+'/'+new_name) #rename new file in path
        
    except:
        continue
kgsdhlau

kgsdhlau3#

for z in range(len(link)): 
    try:
        driver.get(link[z])
        driver.set_page_load_timeout(10)

        # create the file name from the link, like below:
        file_name = link[z].replace("/", "-")

        # add the code to download the pdf file

        # rename the downloaded file
        os.rename('<downloaded file name with path>', file_name <with path>)
    except:
        continue

相关问题