微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

BeautifulSoup 和 Selenium:从页面下载文件

如何解决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 举报,一经查实,本站将立刻删除。