python - Python WebScrape .xls 文件到 PowerBi
问题描述
请,我需要以下代码的帮助。
代码登录下面的网站并移动到包含多个 .xls、.csv 和 .pdf 文件的选项卡
我想:
- 从对应于 <td <i 标签的表“Diretorio”和“Arquivo”下载文件。
- 构建一个 While true 循环以获取所有文件,尽管它们的名称
- “Diretorio”表上的每个链接都会打开右侧表“Arquivo”上的其他不同文件
- 将文件连接到一个文件夹并映射到熊猫数据框
- 使用下载的文件的映射创建熊猫数据框表
- 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 文件?
解决方案
推荐阅读
- python - 使用 jupyter notebook 时无法在 ply 中构建词法分析器
- python - allennlp.common.JsonDict,TypeError:FileLock.acquire:`poll_intervall` 不存在
- spring - 使用spring注解@Transactional(rollbackFor = Exception.class)不能回滚事务,发生异常时
- python - 如何让 mypy 记住 hasattr?
- vim - 在 vim 状态行显示总字符数
- java - java - 如何有效地计算两个时间戳之间的持续时间,包括java中的日期?
- django - 如何检查应用程序发送的 ACCESS_KEY_SECRET 和 ACCESS_KEY_ID 以及这些凭据也存储在应用程序表中
- javascript - 运行 selenium js 客户端
- android - Android DNS-SD 支持 Subtypes?
- sql - INNER JOIN 和计数 POSTGRESQL