我正在尝试创建一个scraper,该scraper进入网站并下载一个包含房地产基金资产负债表的XML文件。
使用下面的代码,我输入了一个特定基金的网站,文档编号为07000400000146(在URL的末尾(,用网站上的搜索栏过滤文档,然后使用XPath点击下载表中的第一个文档。
driver.get('https://fnet.bmfbovespa.com.br/fnet/publico/abrirGerenciadorDocumentosCVM?cnpjFundo=07000400000146')
driver.find_element_by_css_selector(f'input[type="search"]').click()
driver.find_element_by_css_selector(f'input[type="search"]').send_keys('informe mensal')
time.sleep(1)
driver.find_element_by_xpath('//*[@id="tblDocumentosEnviados"]/tbody/tr[1]/td[10]/div/a[2]/i').click()
我如何创建一个布尔表达式来发现一行是2020年3月还是2020年2月或2020年1月;参考数据;然后下载每个相应日期的所有可用文件?
您可以尝试类似datelist=[ '03/2020','02/2020','01/2020']
这样的列表,并使用下面的xpath选项检查网页上的日期是否可用。如果日期可用,则会单击并下载,否则将显示无可用日期。使用try..except
块。忽略sleep()
使用WebDriverWait
((
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
driver=webdriver.Chrome()
driver.get('https://fnet.bmfbovespa.com.br/fnet/publico/abrirGerenciadorDocumentosCVM?cnpjFundo=07000400000146')
driver.find_element_by_css_selector('input[type="search"]').click()
driver.find_element_by_css_selector('input[type="search"]').send_keys('informe mensal')
datelist=[ '03/2020','02/2020','01/2020']
for dates in datelist:
try:
WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.XPATH,"//table[@id='tblDocumentosEnviados']//td[text()='"+ dates + "']/following-sibling::td[5]//a[@title='Download do Documento']"))).click()
print('file downloaded with dates available ' + dates)
except:
print("No such dates available " + dates)
这将像这样在控制台上打印。
No such dates available 03/2020
file downloaded with dates available 02/2020
file downloaded with dates available 01/2020
更新代码,状态为Ativo
driver.get('https://fnet.bmfbovespa.com.br/fnet/publico/abrirGerenciadorDocumentosCVM?cnpjFundo=07000400000146')
driver.find_element_by_css_selector('input[type="search"]').click()
driver.find_element_by_css_selector('input[type="search"]').send_keys('informe mensal')
datelist=[ '03/2020','02/2020','01/2020']
for dates in datelist:
try:
WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.XPATH,"//table[@id='tblDocumentosEnviados']//td[text()='"+ dates +"']/following-sibling::td[.//span[text()='Ativo']]/following-sibling::td//a[@title='Download do Documento']"))).click()
print('file downloaded with dates available ' + dates)
except:
print("No such dates available " + dates)
请尝试以下代码下载pdf
driver.get('https://fnet.bmfbovespa.com.br/fnet/publico/abrirGerenciadorDocumentosCVM?cnpjFundo=07000400000146')
driver.maximize_window()
wait = WebDriverWait(driver, 20)
elemnt=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'label > input')))
elemnt.send_keys('informe mensal')
mylist=[ '03/2020','02/2020','01/2020']
for list in mylist:
button = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH,
"//td[text()='" + list + "']//following-sibling::td//a[@title='Download do Documento']")))
driver.execute_script("arguments[0].click();", button);