draft version of crawler frame work with config implementation

This commit is contained in:
Shaghayegh
2023-03-10 11:41:34 +01:00
parent 68f8c51547
commit 6a3170425b
17 changed files with 42290 additions and 25207 deletions
+40 -174
View File
@@ -1,9 +1,11 @@
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.common.exceptions import TimeoutException, NoSuchElementException, StaleElementReferenceException, WebDriverException
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
@@ -16,194 +18,58 @@ class ScrapeWithSelenium:
# self.options.add_argument('--headless')
self.driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
self.driver.maximize_window()
self.urls = urls
self.urls = urls if urls else []
self.items = []
self.config = ConfigParser()
with codecs.open(os.getcwd() + './Bachelorarbeit/EU_xpath_config.ini', 'r', encoding='utf-8') as f :
self.config.read_file(f)
self.run()
def scrape(self, url):
self.driver.get(url)
item = {}
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:
WebDriverWait(self.driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, "//body")))
except TimeoutException as e:
logging.error(f"TimeoutException: {e}. Unable to load the page at URL: {url}")
return None
except WebDriverException as e:
logging.error(f"WebDriverException: {e}")
return None
for field_name, xpath in self.config.items('fields'):
try:
WebDriverWait(self.driver, 1).until(EC.presence_of_element_located((By.XPATH, xpath)))
except NoSuchElementException as e:
logging.error(f"NoSuchElementException: {e}. Unable to find element with xpath {xpath} on page: {url}")
item[field_name] = ""
continue
except StaleElementReferenceException as e:
logging.error(f"StaleElementReferenceException: {e}. The element with XPATH {xpath} is no longer attached to the DOM on page: {url}")
item[field_name] = ""
continue
except TimeoutException as e:
logging.error(f"TimeoutException: {e}. Unable to load the xpath {xpath} at URL: {url}")
item[field_name] = ""
continue
output_list = self.driver.find_elements(By.XPATH, xpath)
if len(output_list) == 1:
item[field_name] = output_list[0].text
else:
item[field_name] = [output.text for output in output_list]
return item
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.items.append(result)
df = pd.DataFrame(self.items)
df.to_json(self.config['EU']['data_set'], orient='records', force_ascii=False)
self.driver.quit()