104 lines
3.2 KiB
Python
104 lines
3.2 KiB
Python
# ------------------------- Overview -------------------- #
|
|
'''
|
|
link_extractor.py uses a selenium ChromeDriverManager to locate
|
|
all the project links available on a particular website.
|
|
This project links will be stored in a json file using pandas DataFrame
|
|
The xpath implimentation is stored in links_config.ini.
|
|
'''
|
|
# ---------------------------------------------------------#
|
|
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
|
|
from configparser import ConfigParser
|
|
import os
|
|
import time
|
|
import pandas as pd
|
|
|
|
|
|
options = webdriver.ChromeOptions()
|
|
options.add_argument('--disable-cookies')
|
|
# options.add_argument('--headless')
|
|
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
|
|
driver.delete_all_cookies()
|
|
|
|
config = ConfigParser()
|
|
config.read(os.getcwd() + './links_config.ini')
|
|
|
|
url = config['example']['start-url']
|
|
driver.get(url)
|
|
|
|
driver.maximize_window()
|
|
|
|
# ----------------------------- Coockies ---------------------------------- #
|
|
|
|
consent_btn= WebDriverWait(driver,10).until(EC.presence_of_element_located((
|
|
By.XPATH, config['example']['cookies']
|
|
)))
|
|
if consent_btn:
|
|
consent_btn.click()
|
|
|
|
# ----------------------------- locate search box -> type the search word -> click on search button ---------------------------------- #
|
|
search_input = driver.find_element(By.XPATH, config['example']['search-box'])
|
|
search_input.send_keys("Universität Duisburg-Essen")
|
|
time.sleep(0.1)
|
|
search_btn = driver.find_element(By.XPATH, config['example']['search-btn'])
|
|
search_btn.click()
|
|
|
|
# ----------------------------- Pagination ---------------------------------- #
|
|
|
|
links = []
|
|
isNextDisabeled = False
|
|
|
|
while not isNextDisabeled:
|
|
|
|
try:
|
|
|
|
links_config = config['example']
|
|
|
|
projects_loading_phase = WebDriverWait(driver,10).until(EC.presence_of_element_located((
|
|
By.XPATH, links_config['loading-project-data']
|
|
)))
|
|
|
|
container = driver.find_element(By.XPATH, links_config['projects-container'])
|
|
projects = container.find_elements(By.XPATH, links_config['projects-list'])
|
|
for project in projects:
|
|
links.append(project.find_element(By.XPATH, links_config['project']).get_attribute("href"))
|
|
|
|
# Saving data into a json file:
|
|
df_links = pd.DataFrame({'project_link':links})
|
|
df_links.to_json("project_links_example.json", orient='records')
|
|
|
|
try:
|
|
next_btn = WebDriverWait(driver, 10).until(EC.presence_of_element_located(
|
|
(By.XPATH, links_config['next-btn'])
|
|
))
|
|
|
|
|
|
if "disabled" in next_btn.get_attribute('class'):
|
|
isNextDisabeled = True
|
|
|
|
elif next_btn:
|
|
driver.find_element(By.XPATH, links_config['next-btn']).click()
|
|
|
|
else:
|
|
isNextDisabeled = True
|
|
except:
|
|
isNextDisabeled = True
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
print(e, "main error")
|
|
isNextDisabeled = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
driver.close() |