首页 > 解决方案 > Python WebScrape .xls 文件到 PowerBi

问题描述

请,我需要以下代码的帮助。

代码登录下面的网站并移动到包含多个 .xls、.csv 和 .pdf 文件的选项卡

在此处输入图像描述

我想:

  1. 从对应于 <td <i 标签的表“Diretorio”和“Arquivo”下载文件。
    • 构建一个 While true 循环以获取所有文件,尽管它们的名称
    • “Diretorio”表上的每个链接都会打开右侧表“Arquivo”上的其他不同文件
  2. 将文件连接到一个文件夹并映射到熊猫数据框
    • 使用下载的文件的映射创建熊猫数据框表
    • Dataframe 必须包含 3 列:下载日期、文件名、文件网站完整下载路径

我希望使用 PowerBi 中的 Python 并阅读那里的所有文件。阅读后,我将在 PowerBi 中连接它们并分析数据

import time
from bs4 import BeautifulSoup
from pandas import DataFrame
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Chrome(executable_path='C:\Program Files (x86)\Google\Chrome\chromedriver.exe')
url = "https://website"

driver.get(url)

WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.ID, "i0116")))
driver.find_element_by_id("i0116").send_keys("login")

WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.ID, "idSIButton9")))
driver.find_element_by_id("idSIButton9").click()

time.sleep(1)

driver.find_element_by_id("i0118").send_keys("password*")
WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.ID, "idSIButton9")))
driver.find_element_by_id("idSIButton9").click()

WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.ID, "idSIButton9")))
driver.find_element_by_id("idSIButton9").click()

time.sleep(2)
driver.get(url)

soup = BeautifulSoup(driver.page_source, 'html.parser')
elements = []

# work till here
# after here I need to download all files and maybe put them into a panda dataframe
# I will use it to concatenate all docuements while reading them though powerbi

elements = driver.find_elements_by_class_name("fa fa-file")

try:
    for elem in elements:
        WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.CLASS_NAME, "fa fa-file")))
        driver.find_element_by_class_name("fa fa-file").click()
finally:
    pass

# Close google Chrome
driver.quit()
# Create Pandas DataFrame
headers = elements.pop(0)
df = DataFrame(elements, columns=headers)
df

任何人都知道我如何阅读/下载所有 .xls 文件?

标签: pythonpandasseleniumweb-scrapingwebdriver

解决方案


推荐阅读