0

我正在尝试使用 python 脚本在此网站上下载 CSV 文件:https : //enroyd.com/Sentiment/,以便随后使用 python 分析结果。单击链接时会生成该文件。

我已经尝试了很多在线解决方案,但似乎都没有奏效。对于开放式问题,我很抱歉,但有人有什么建议吗?

我试过这个脚本,但它不起作用

import os
from selenium import webdriver

browser_profile = webdriver.FirefoxProfile('path')

# add the file_formats to download
file_formats = ','.join(["text/plain",
                         "application/pdf",
                         "application/x-pdf",
                         "application/force-download"])

preferences = {
    "browser.download.folderList": 2,
    "browser.download.manager.showWhenStarting": False,
    "browser.download.dir": os.getcwd(),  # will download to current directory
    "browser.download.alertOnEXEOpen": False,
    "browser.helperApps.neverAsk.saveToDisk": file_formats,
    "browser.download.manager.focusWhenStarting": False,
    "browser.helperApps.alwaysAsk.force": False,
    "browser.download.manager.showAlertOnComplete": False,
    "browser.download.manager.useWindow": False,
    "services.sync.prefs.sync.browser.download.manager.showWhenStarting": False,
    "pdfjs.disabled": True
}

for pref, val in preferences.items():
    browser_profile.set_preference(pref, val)

browser_binary = webdriver.firefox.firefox_binary.FirefoxBinary()
browser = webdriver.Firefox(firefox_binary=browser_binary,
                            firefox_profile=browser_profile)

# set the file name that will be saved as when you download is complete
file_name = 'data.csv'

# goto the link to download the file from it will be automatically
# downloaded to the current directory
file_url = 'https://enroyd.com/Sentiment'
browser.find_element_by_class_name('dt-button buttons-csv buttons-html5 DTTT_button DTTT_button_csv').click()

# verify if the expected file name exists in the current directory
path = os.path.join(os.getcwd(), file_name)
assert os.path.isfile(path)
4

0 回答 0