Skip to content

Instantly share code, notes, and snippets.

@Rofram
Last active March 2, 2023 06:37
Show Gist options
  • Save Rofram/113f053f04f32d5b6ee6a9b16c3b99a1 to your computer and use it in GitHub Desktop.
Save Rofram/113f053f04f32d5b6ee6a9b16c3b99a1 to your computer and use it in GitHub Desktop.
cns selenium
import os
from datetime import date
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.webdriver import WebDriver
from openpyxl import Workbook
class DownloadCNJDDocuments:
def __init__(self) -> None:
self.downloadFilepath = os.path.join(os.getcwd(), 'documents')
self.cnj_url = 'https://www.cnj.jus.br/corregedoria/justica_aberta/?'
self.chrome = self._new_chrome_browser(headless=False, downloadPath=self.downloadFilepath)
def execute(self) -> None:
self._access_url(self.cnj_url)
self._access_extrajudicial_services()
self._search_cns()
self._download_pdf()
self.chrome.quit()
def _access_url(self, url) -> None:
self.chrome.get(url)
def _access_extrajudicial_services(self) -> None:
anchor = self.chrome.find_element(By.LINK_TEXT, "Clique aqui")
anchor.click()
def _search_cns(self) -> None:
input = self.chrome.find_element(By.ID, "COD_IDENTIFICACAO_UNICA")
input.send_keys("02.675-7")
submit_button = self.chrome.find_element(By.CSS_SELECTOR, 'button[type="submit"]')
submit_button.click()
def _generate_excel_spreadsheet(self) -> None:
# Encontre a linha (tr) específica da tabela usando seu id
tr = self.chrome.find_element(By.ID, "8124")
# Crie um novo arquivo Excel e adicione uma planilha
workbook = Workbook()
planilha = workbook.active
# Encontre todas as linhas da tabela usando um seletor CSS ou XPATH
linhas = tr.find_elements(By.TAG_NAME,"tr")
# Itere sobre as linhas da tabela e adicione-as como linhas na planilha
for linha in linhas:
celulas = linha.find_elements(By.TAG_NAME,"td")
linha_dados = []
# Itere sobre as células da linha e adicione-as como valores nas colunas da planilha
for celula in celulas:
linha_dados.append(celula.text)
planilha.append(linha_dados)
# Salve o arquivo Excel
workbook.save(filename="tabela.xlsx")
def _download_pdf(self) -> None:
cns_link = self.chrome.find_element(By.CSS_SELECTOR, 'img[src="html/img/produtividade.png"]')
cns_link.click()
cns_link.click()
download_pdf_link = self.chrome.find_element(By.CSS_SELECTOR, 'a>strong')
download_pdf_link.click()
sleep(2)
self._rename_downloaded_file()
def _rename_downloaded_file(self) -> None:
old_file = os.path.join(self.downloadFilepath, 'out.php')
new_file = os.path.join(self.downloadFilepath, f'{date.today().strftime("%d-%m-%Y")}-cns.pdf')
os.rename(old_file, new_file)
def _new_chrome_browser(self, headless=True, downloadPath=None) -> WebDriver:
""" Helper function that creates a new Selenium browser """
options = webdriver.ChromeOptions()
if headless:
options.add_argument('headless')
if downloadPath is not None:
prefs = {}
is_download_folder_exist = os.path.isdir(downloadPath)
if not is_download_folder_exist:
os.makedirs(downloadPath)
prefs["profile.default_content_settings.popups"] = 0
prefs["download.default_directory"] = downloadPath
prefs["plugins.always_open_pdf_externally"] = True
options.add_experimental_option("prefs", prefs)
browser = webdriver.Chrome(options=options)
browser.implicitly_wait(3)
return browser
if __name__ == '__main__':
app = DownloadCNJDDocuments()
app.execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment