BeautifulSoup 和 Selenium:从页面下载文件

问题描述

我使用 Selenium 导航到 URL,因为 beautifulsouphtml.parser 不会输出所有内容。我收集了页面上的文件列表并将它们存储在一个数组中。接下来,我想遍历每个并将所有内容写入 CSV,但文件路径使用 /file-browser-api/download/ 格式。

有没有办法打开每个文件然后写入 CSV 文件?从技术上讲,我可以使用 Selenium 来单击每一个,但该包不支持迭代。

url = "https://marketplace.spp.org/pages/rtbm-lmp-by-location#%2F2021%2F02%2FRePrice"
driver.get(url)
driver.maximize_window()

html = driver.page_source
soup = BeautifulSoup(html,"lxml")

all_files = []
files = soup.find_all(class_="files")
for file in files:
    tests = file.get('href')
    if tests != None:
        all_files.append(tests)

#this was a test to see if I Could download the first file
with open(all_files[0],'wb') as f:
    writer = csv.writer(f)
    writer.writerows(headers)
    writer.writerows(row for row in rows if row)
[Errno 2] No such file or directory: '/file-browser-api/download/rtbm-lmp-by-location?path=%2F2021%2F02%2FRePrice%2FRTBM-LMP-SL-202102051005-R1-RC4.csv'

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)