python - Python从网站上的按钮下载文件
问题描述
我试图使用请求库从该网站下载文件,但我不知道如何激活按钮以使用某些日期参数下载“XLS”文件。
我想在不使用浏览器的情况下下载此文件。
检查按钮时,我得到以下代码:
<button class="btn btn-primary" id="btnGerarPrecoHorarioXls" onclick="gerarPrecoHorarioXls();return false;">Gerar Arquivo <i aria-hidden="true" class="fa fa-file-excel-o fa-2"> </i></button>
激活按钮时,似乎在网络选项卡上启动了一个 js。
但我不知道如何插入参数所以它返回文件
解决方案
您可以使用 Selenium 单击网页上的项目。例如:
from selenium import webdriver
import time
options = webdriver.ChromeOptions()
button = driver.find_elements_by_xpath("Right click on the item, choose inspect, then right click, copy xpath and put it in here.")[0]
button.click()