123 lines
6.1 KiB
Python
123 lines
6.1 KiB
Python
from selenium import webdriver
|
|
from selenium.webdriver.chrome.service import Service
|
|
from webdriver_manager.chrome import ChromeDriverManager
|
|
from selenium.webdriver.common.by import By
|
|
from selenium.webdriver.support.wait import WebDriverWait
|
|
from selenium.webdriver.support import expected_conditions as EC
|
|
import logging
|
|
from configparser import ConfigParser
|
|
import codecs
|
|
import os
|
|
import pandas as pd
|
|
from selenium.common.exceptions import TimeoutException, NoSuchElementException, StaleElementReferenceException, ElementClickInterceptedException, ElementNotInteractableException
|
|
|
|
|
|
class ProjectLinksSelenium:
|
|
def __init__(self, start_urls, website_name, config_link, body_wait_time, xpath_wait_time):
|
|
self.start_urls = start_urls
|
|
self.website_name = website_name
|
|
self.config_link = config_link
|
|
self.body_wait_time = body_wait_time
|
|
self.xpath_wait_time = xpath_wait_time
|
|
self.config = ConfigParser()
|
|
with codecs.open(os.getcwd() + config_link, 'r', 'utf-8') as f:
|
|
self.config.read_file(f)
|
|
self.options = webdriver.ChromeOptions()
|
|
self.options.add_argument('--headless')
|
|
self.options.add_argument('--disable-cookies')
|
|
self.options.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3')
|
|
self.driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=self.options)
|
|
self.driver.delete_all_cookies()
|
|
self.isNextDisabeled = False
|
|
self.project_links = []
|
|
self.links_config = self.config[self.website_name]
|
|
self.driver.get(self.start_urls)
|
|
self.driver.maximize_window()
|
|
self.fill_out_search_form()
|
|
|
|
|
|
def fill_out_search_form(self):
|
|
if 'search-box' in self.links_config and 'search-btn' in self.links_config:
|
|
try:
|
|
search_box = self.driver.find_element(By.XPATH, self.links_config['search-box'])
|
|
search_keyword = self.links_config['search-keyword']
|
|
if search_keyword:
|
|
search_box.send_keys(search_keyword)
|
|
else:
|
|
search_box.send_keys('Universität Duisburg-Essen')
|
|
|
|
try:
|
|
WebDriverWait(self.driver, self.xpath_wait_time).until(EC.presence_of_element_located((By.XPATH, self.links_config['search-btn'])))
|
|
search_button = self.driver.find_element(By.XPATH, self.links_config['search-btn'])
|
|
search_button.click()
|
|
except (TimeoutException, NoSuchElementException, ElementNotInteractableException ) as e:
|
|
logging.error(f'Timeout waiting for search button to be clickable: {e}')
|
|
return
|
|
try:
|
|
WebDriverWait(self.driver, self.body_wait_time).until(EC.presence_of_element_located((By.XPATH, self.links_config['loading-body'])))
|
|
except TimeoutException:
|
|
logging.error('Timeout waiting for page to load after clicking search button')
|
|
return
|
|
|
|
self.extract_project_links()
|
|
|
|
except (NoSuchElementException, ElementNotInteractableException) as e:
|
|
logging.error(f'Error finding the form elements: {e}')
|
|
|
|
else:
|
|
logging.info('No search form found in config file. Extracting project links...')
|
|
self.extract_project_links()
|
|
|
|
|
|
|
|
def extract_project_links(self):
|
|
while not self.isNextDisabeled:
|
|
try:
|
|
WebDriverWait(self.driver,self.body_wait_time).until(EC.presence_of_element_located((By.XPATH, self.links_config['loading-body'])))
|
|
container = self.driver.find_element(By.XPATH, self.links_config['projects-container'])
|
|
projects = container.find_elements(By.XPATH, self.links_config['projects-list'])
|
|
# Extracting project links from a webpage
|
|
for project in projects:
|
|
try:
|
|
self.project_links.append(project.find_element(By.XPATH, self.links_config['project-link']).get_attribute("href"))
|
|
except StaleElementReferenceException as e:
|
|
logging.error(f"The reference to the element in the DOM is no longer valid: {e}")
|
|
continue
|
|
# Pagination
|
|
try:
|
|
WebDriverWait(self.driver, self.xpath_wait_time).until(EC.presence_of_element_located((By.XPATH, self.links_config['next-btn'])))
|
|
WebDriverWait(self.driver, self.xpath_wait_time).until(EC.element_to_be_clickable((By.XPATH, self.links_config['next-btn'])))
|
|
|
|
next_btn = self.driver.find_element(By.XPATH, self.links_config['next-btn'])
|
|
next_btn_class = next_btn.get_attribute('class')
|
|
|
|
if 'next_disable_condition' in self.links_config and self.links_config['next_disable_condition'] in next_btn_class:
|
|
self.isNextDisabeled = True
|
|
else:
|
|
self.driver.execute_script("arguments[0].click();", next_btn)
|
|
|
|
except NoSuchElementException as e:
|
|
logging.error(f"NoSuchElementException: {e}")
|
|
self.isNextDisabeled = True
|
|
|
|
except TimeoutException as e:
|
|
logging.error(f"TimeoutException: {e}")
|
|
self.isNextDisabeled = True
|
|
|
|
except ElementClickInterceptedException as e:
|
|
logging.error(f"Button not clickable: {e}")
|
|
self.isNextDisabeled = True
|
|
|
|
|
|
except (TimeoutException, StaleElementReferenceException) as e:
|
|
logging.error(f"Failed to locate project container: {e}")
|
|
self.isNextDisabeled = True
|
|
|
|
self.save_project_links()
|
|
|
|
def save_project_links(self):
|
|
self.driver.quit()
|
|
df = pd.DataFrame(self.project_links, columns=['project_link'])
|
|
df.to_json(f'project_links_{self.website_name}.json', orient='records')
|
|
|
|
|