将 .xls 文件保存在特定文件夹中

发布于 2025-01-20 05:24:19 字数 3136 浏览 3 评论 0原文

我有一个Web_scraping脚本,该脚本生成下载,从而导致 一个.xls文件

我无法将此.xls文件保存在我想要的文件夹中。

我想将其保存到本地文件夹

我尝试过这样的尝试:

import time 
from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select, WebDriverWait
from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

driver = webdriver.Chrome(ChromeDriverManager().install())
driver = webdriver.Chrome()

options = Options()
options.use_chromium = True
options.add_argument("--window-size=1920,1080")
options.add_argument("disable-gpu")

options.add_experimental_option('prefs', {
  'download.default_directory': r'C:\Users\prmatteo\OneDrive - xxxS.A\Área de Trabalho\arquivos python'
})

driver.get('http://estatisticas.cetip.com.br/astec/series_v05/paginas/lum_web_v05_series_introducao.asp?str_Modulo=Ativo&int_Idioma=1&int_Titulo=6&int_NivelBD=2/')
driver.find_element_by_xpath('//*[@id="divContainerIframeBmf"]/div/dl/dd[2]/a').click()
time.sleep(3)
driver.switch_to.frame(driver.find_element(By.XPATH, '//iframe[@name="dados_corpo"]'))
driver.switch_to.frame(driver.find_element(By.XPATH, '//frame[@name="ativo"]'))
find_dp1 = driver.find_element(By.XPATH, '//select[@name="ativo"]')
select_find_dp1 = Select(find_dp1)
select_find_dp1.select_by_visible_text("CBIO - Crédito de descarbonização")
time.sleep(3)

driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element(By.ID, 'dados_corpo'))
driver.switch_to.frame(driver.find_element(By.TAG_NAME, 'frameset').find_elements(By.TAG_NAME, 'frame')[1])

time.sleep(1)
informacoes = Select(driver.find_element(By.NAME, 'selectopcoes'))
informacoes.select_by_visible_text('Estoque')

driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element(By.ID, 'dados_corpo'))
driver.switch_to.frame(driver.find_element(By.TAG_NAME, 'frameset').find_elements(By.TAG_NAME, 'frame')[2])

time.sleep(1)
# Data Inicial 
driver.find_element(By.NAME, 'DT_DIA_DE').send_keys('10')
driver.find_element(By.NAME, 'DT_MES_DE').send_keys('10')
driver.find_element(By.NAME, 'DT_ANO_DE').send_keys('2021')

# Data Final
driver.find_element(By.NAME, 'DT_DIA_ATE').send_keys('30')
driver.find_element(By.NAME, 'DT_MES_ATE').send_keys('12')
driver.find_element(By.NAME, 'DT_ANO_ATE').send_keys('2022')

driver.find_elements(By.CLASS_NAME, 'button')[1].click()

driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element(By.TAG_NAME, 'iframe'))
time.sleep(1)
driver.find_element(By.CLASS_NAME, 'primary-text').find_element(By.TAG_NAME,'a').click()

fiperizeiestecódigopara tentardirecioná-lo para uma面食:

  options.add_experimental_option('prefs', {
      'download.default_directory': r'C:\Users\prmatteo\OneDrive - xxxS.A\Área de Trabalho\arquivos python'
    })

porém,nãofuncionou。

I have a web_scraping script, which generates a download, resulting in
a .xls file

I can't save this .xls file in the folder I want.

I would like to save it to a local folder

I tried like this:

import time 
from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select, WebDriverWait
from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

driver = webdriver.Chrome(ChromeDriverManager().install())
driver = webdriver.Chrome()

options = Options()
options.use_chromium = True
options.add_argument("--window-size=1920,1080")
options.add_argument("disable-gpu")

options.add_experimental_option('prefs', {
  'download.default_directory': r'C:\Users\prmatteo\OneDrive - xxxS.A\Área de Trabalho\arquivos python'
})

driver.get('http://estatisticas.cetip.com.br/astec/series_v05/paginas/lum_web_v05_series_introducao.asp?str_Modulo=Ativo&int_Idioma=1&int_Titulo=6&int_NivelBD=2/')
driver.find_element_by_xpath('//*[@id="divContainerIframeBmf"]/div/dl/dd[2]/a').click()
time.sleep(3)
driver.switch_to.frame(driver.find_element(By.XPATH, '//iframe[@name="dados_corpo"]'))
driver.switch_to.frame(driver.find_element(By.XPATH, '//frame[@name="ativo"]'))
find_dp1 = driver.find_element(By.XPATH, '//select[@name="ativo"]')
select_find_dp1 = Select(find_dp1)
select_find_dp1.select_by_visible_text("CBIO - Crédito de descarbonização")
time.sleep(3)

driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element(By.ID, 'dados_corpo'))
driver.switch_to.frame(driver.find_element(By.TAG_NAME, 'frameset').find_elements(By.TAG_NAME, 'frame')[1])

time.sleep(1)
informacoes = Select(driver.find_element(By.NAME, 'selectopcoes'))
informacoes.select_by_visible_text('Estoque')

driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element(By.ID, 'dados_corpo'))
driver.switch_to.frame(driver.find_element(By.TAG_NAME, 'frameset').find_elements(By.TAG_NAME, 'frame')[2])

time.sleep(1)
# Data Inicial 
driver.find_element(By.NAME, 'DT_DIA_DE').send_keys('10')
driver.find_element(By.NAME, 'DT_MES_DE').send_keys('10')
driver.find_element(By.NAME, 'DT_ANO_DE').send_keys('2021')

# Data Final
driver.find_element(By.NAME, 'DT_DIA_ATE').send_keys('30')
driver.find_element(By.NAME, 'DT_MES_ATE').send_keys('12')
driver.find_element(By.NAME, 'DT_ANO_ATE').send_keys('2022')

driver.find_elements(By.CLASS_NAME, 'button')[1].click()

driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element(By.TAG_NAME, 'iframe'))
time.sleep(1)
driver.find_element(By.CLASS_NAME, 'primary-text').find_element(By.TAG_NAME,'a').click()

Utilizei este código para tentar direcioná-lo para uma pasta:

  options.add_experimental_option('prefs', {
      'download.default_directory': r'C:\Users\prmatteo\OneDrive - xxxS.A\Área de Trabalho\arquivos python'
    })

Porém, não funcionou.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩心态 2025-01-27 05:24:19

将文件从一个目录复制或将文件移动到另一个目录:

import shutil
 
# source path
source = "/old_docs/file.xls"
 
# destination path
destination = "/new_docs/file.xls"
# copy file 
shutil.copy(source, destination)
# move file
shutil.move(source, destination)

以变量为变量:移动或复制文件:

filename = "file.xls"

# source path
source = "/old_docs/"
 
# destination path
destination = "/new_docs/"

# copy file
shutil.copy(source + filename, destination + filename)

# move file
shutil.move(source + filename, destination + filename)

Copy or move a file from one directory to another:

import shutil
 
# source path
source = "/old_docs/file.xls"
 
# destination path
destination = "/new_docs/file.xls"
# copy file 
shutil.copy(source, destination)
# move file
shutil.move(source, destination)

Move or copy file with filename as variable:

filename = "file.xls"

# source path
source = "/old_docs/"
 
# destination path
destination = "/new_docs/"

# copy file
shutil.copy(source + filename, destination + filename)

# move file
shutil.move(source + filename, destination + filename)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文