209 lines
8.3 KiB
Python
209 lines
8.3 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
|
|
from configparser import ConfigParser
|
|
import codecs
|
|
import os
|
|
import pandas as pd
|
|
|
|
|
|
class ScrapeWithSelenium:
|
|
def __init__(self, urls):
|
|
# self.options = webdriver.ChromeOptions()
|
|
# self.options.add_argument('--headless')
|
|
self.driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
|
|
self.driver.maximize_window()
|
|
self.urls = urls
|
|
self.config = ConfigParser()
|
|
self.run()
|
|
|
|
|
|
def scrape(self, url):
|
|
|
|
self.driver.get(url)
|
|
|
|
with codecs.open(os.getcwd() + './Bachelorarbeit/data_config.ini', 'r', encoding='utf-8') as f :
|
|
self.config.read_file(f)
|
|
|
|
|
|
try:
|
|
config_data = self.config['EU']
|
|
try:
|
|
Title_loading_phase = WebDriverWait(self.driver,10).until(EC.presence_of_element_located((
|
|
By.XPATH, config_data['title_loading_phase']
|
|
)))
|
|
title = self.driver.find_element(By.XPATH, config_data['title']).text.strip()
|
|
except:
|
|
pass
|
|
|
|
programme=''
|
|
project_description = ''
|
|
objective =''
|
|
proposal = ''
|
|
topics = ''
|
|
funding_scheme = ''
|
|
coordinator=''
|
|
coordinator_address =''
|
|
coordinator_link= ''
|
|
fields_of_science = []
|
|
coordinator_region = ''
|
|
coordinator_activity_type=''
|
|
coordinator_net_EU_contribution=''
|
|
coordinator_other_fundings=''
|
|
|
|
|
|
Content_container_loading_phase = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((
|
|
By.XPATH, config_data['content_container_loading_phase'])))
|
|
content_container = self.driver.find_element(By.XPATH, config_data['content_container'])
|
|
try:
|
|
programme = content_container.find_element(By.XPATH, config_data['programme']).text.strip()
|
|
except:
|
|
pass
|
|
try:
|
|
objective = content_container.find_element(By.XPATH, config_data['objective']).text.strip()
|
|
except:
|
|
pass
|
|
try:
|
|
project_description = content_container.find_element(By.XPATH, config_data['project_description']).text.strip()
|
|
except:
|
|
pass
|
|
try:
|
|
proposal = content_container.find_element(By.XPATH, config_data['proposal']).text.strip()
|
|
except:
|
|
pass
|
|
try:
|
|
topics = content_container.find_element(By.XPATH, config_data['topics']).text.strip()
|
|
except:
|
|
pass
|
|
try:
|
|
funding_scheme = content_container.find_element(By.XPATH, config_data['funding_scheme']).text.strip()
|
|
except:
|
|
pass
|
|
try:
|
|
coordinator = content_container.find_element(By.XPATH, config_data['coordinator']).text.strip()
|
|
except:
|
|
pass
|
|
try:
|
|
coordinator_address = content_container.find_element(By.XPATH, config_data['coordinator_address']).text.strip()
|
|
except:
|
|
pass
|
|
try:
|
|
coordinator_activity_type = content_container.find_element(By.XPATH, config_data['coordinator_activity_type']).text.strip()
|
|
except:
|
|
pass
|
|
try:
|
|
coordinator_region = content_container.find_element(By.XPATH, config_data['coordinator_region']).text.strip()
|
|
except:
|
|
pass
|
|
try:
|
|
coordinator_net_EU_contribution = content_container.find_element(By.XPATH, config_data['coordinator_net_EU_contribution']).text.strip()
|
|
except:
|
|
pass
|
|
try:
|
|
coordinator_other_fundings = content_container.find_element(By.XPATH, config_data['coordinator_other_fundings']).text.strip()
|
|
except:
|
|
pass
|
|
try:
|
|
coordinator_link = content_container.find_element(By.XPATH, config_data['coordinator_link']).get_attribute('href').strip() #define for loop
|
|
except:
|
|
pass
|
|
try:
|
|
for fields in content_container.find_elements(By.XPATH, config_data['fields_of_science_list']):
|
|
fields_of_science.append(fields.find_element(By.XPATH, config_data['fields_of_science']).text.strip())
|
|
except:
|
|
pass
|
|
# ------------------------------ ptoject info card ------------------------------- #
|
|
project_info_container_loading_phase = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((
|
|
By.XPATH, config_data['project_info_container_loading_phase'])))
|
|
|
|
project_info_container = self.driver.find_element(By.XPATH, config_data['project_info_container'])
|
|
project_acronym=''
|
|
grant_agreement_ID=''
|
|
doi=''
|
|
start_date=''
|
|
end_date=''
|
|
funded_under=''
|
|
EU_contribution=''
|
|
overal_budget=''
|
|
|
|
try:
|
|
project_acronym = project_info_container.find_element(By.XPATH, config_data['project_acronym']).text.strip()
|
|
except:
|
|
pass
|
|
try:
|
|
grant_agreement_ID = project_info_container.find_element(By.XPATH, config_data['grant_agreement_ID']).text.strip()
|
|
except:
|
|
pass
|
|
try:
|
|
doi = project_info_container.find_element(By.XPATH, config_data['doi']).get_attribute('href').strip()
|
|
except:
|
|
pass
|
|
try:
|
|
start_date = project_info_container.find_element(By.XPATH, config_data['start_date']).text.strip()
|
|
except:
|
|
pass
|
|
try:
|
|
end_date = project_info_container.find_element(By.XPATH, config_data['end_date']).text.strip()
|
|
except:
|
|
pass
|
|
try:
|
|
funded_under = project_info_container.find_element(By.XPATH, config_data['funded_under']).text.strip()
|
|
except:
|
|
pass
|
|
try:
|
|
overal_budget = project_info_container.find_element(By.XPATH, config_data['overal_budget']).text.strip()
|
|
except:
|
|
pass
|
|
try:
|
|
EU_contribution = project_info_container.find_element(By.XPATH, config_data['EU_contribution']).text.strip()
|
|
except:
|
|
pass
|
|
|
|
|
|
|
|
|
|
return{
|
|
'Title': title,
|
|
'Programme': programme,
|
|
'Objective' : objective,
|
|
'Project description' : project_description,
|
|
'Proposal': proposal,
|
|
'Topic(s)': topics,
|
|
'Funding scheme': funding_scheme,
|
|
'Coordinator':coordinator,
|
|
'Coordinator_address':coordinator_address,
|
|
'Coordinator_activity_type':coordinator_activity_type,
|
|
'Coordinator_net_EU_contribution':coordinator_net_EU_contribution,
|
|
'Coordinator_region': coordinator_region,
|
|
'Coordinator_other_fundings':coordinator_other_fundings,
|
|
'Coordinator_link':coordinator_link,
|
|
'Fields of science':fields_of_science,
|
|
'Grant agreement ID':grant_agreement_ID,
|
|
'DOI':doi,
|
|
'Project Acronym':project_acronym,
|
|
'Start date': start_date,
|
|
'End date': end_date,
|
|
'Funded under':funded_under,
|
|
'Overal budget':overal_budget,
|
|
'EU contribution':EU_contribution
|
|
|
|
}
|
|
except:
|
|
return None
|
|
|
|
|
|
|
|
def run(self):
|
|
results = []
|
|
for url in self.urls:
|
|
result = self.scrape(url)
|
|
if result:
|
|
results.append(result)
|
|
df = pd.DataFrame(results)
|
|
df.to_json("EU_all_data.json", orient='records', force_ascii=False)
|
|
|
|
def __del__(self):
|
|
self.driver.quit() |