Files
inse-internal-external-scripts/extern/scopus/scopus_data.py
T
2024-04-08 09:44:52 +02:00

452 lines
21 KiB
Python

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.firefox.options import Options
from random import randint
import time
import sys
from elasticsearch import Elasticsearch
import json
import os
es = Elasticsearch({'host':'inse.nes.uni-due.de', 'port': 443, 'path_prefix':'elastic', 'scheme':'https'}, basic_auth=('elastic', 'RLldUaJgbGeoqd3KCX3p'), request_timeout=60, max_retries=5, retry_on_timeout=True)
index_name = "scopus_persons_1.0"
def getData(driver, links):
print("Getting Data")
# start log
log = {}
log_name = "scopus_data_crawler_log.json"
if os.path.exists(log_name):
# if json exists -> read in file
with open(log_name, 'r') as jsonfile:
log = json.load(jsonfile)
else:
# if json doesn't exist -> #1 create json -> #2 write in initial structure -> #3 read it in
# 1
open(log_name, "x")
# 2
initial_dict = {"done": []}
with open(log_name, "w") as jsonfile:
jsonfile.write(json.dumps(initial_dict))
# 3
with open(log_name, 'r') as jsonfile:
log = json.load(jsonfile)
for link in links:
l = link["link"]
print("- - - " + l)
if l in log["done"]:
print("- - - - - - Already crawled!")
continue
try:
driver.get(l)
time.sleep(randint(10, 15))
# check if site is loaded -> if not: try again once
try:
driver.find_element(By.XPATH, "/html/body/div/div/div[1]/div[2]/div/div[3]/div[10]/div[5]/form/micro-ui/scopus-author-profile-page-control-microui/div[1]/div[1]/div/div[1]/div/h1/strong").text
except NoSuchElementException as nsee:
time.sleep(20)
try:
driver.find_element(By.XPATH, "/html/body/div/div/div[1]/div[2]/div/div[3]/div[10]/div[5]/form/micro-ui/scopus-author-profile-page-control-microui/div[1]/div[1]/div/div[1]/div/h1/strong").text
except NoSuchElementException as nsee:
continue
# get informations
lsf_id = link["lsf_id"]
url = driver.current_url
name = driver.find_element(By.XPATH, "/html/body/div/div/div[1]/div[2]/div/div[3]/div[10]/div[5]/form/micro-ui/scopus-author-profile-page-control-microui/div[1]/div[1]/div/div[1]/div/h1/strong").text
firstname = name[name.find(", ") + 2:]
surname = name[:name.find(", ")]
scopus_id = driver.find_element(By.XPATH, "/html/body/div/div/div[1]/div[2]/div/div[3]/div[10]/div[5]/form/micro-ui/scopus-author-profile-page-control-microui/div[1]/div[1]/div/div[1]/ul/li[2]/div/span[2]").text
citations = driver.find_element(By.XPATH, "/html/body/div/div/div[1]/div[2]/div/div[3]/div[10]/div[5]/form/micro-ui/scopus-author-profile-page-control-microui/div[1]/div[1]/div/div[1]/section/div/div[1]/div/div/div/div[1]/span").text
citations = citations.replace(",", "")
citations_in_docs = driver.find_element(By.XPATH, "/html/body/div/div/div[1]/div[2]/div/div[3]/div[10]/div[5]/form/micro-ui/scopus-author-profile-page-control-microui/div[1]/div[1]/div/div[1]/section/div/div[1]/div/div/div/div[2]/span/p/span/em/strong").text
citations_in_docs = citations_in_docs[:citations_in_docs.find(" ")].replace(",", "")
documents = driver.find_element(By.XPATH, "/html/body/div/div/div[1]/div[2]/div/div[3]/div[10]/div[5]/form/micro-ui/scopus-author-profile-page-control-microui/div[1]/div[1]/div/div[1]/section/div/div[2]/div/div/div/div[1]/span").text
documents = documents.replace(",", "")
h_index = driver.find_element(By.XPATH, "/html/body/div/div/div[1]/div[2]/div/div[3]/div[10]/div[5]/form/micro-ui/scopus-author-profile-page-control-microui/div[1]/div[1]/div/div[1]/section/div/div[3]/div/div/div/div[1]/span").text
h_index = h_index.replace(",", "")
# get topics
topics = []
topics_docs = []
topics_fwci = []
try:
driver.find_element(By.XPATH, "/html/body/div/div/div[1]/div[2]/div/div[3]/div[10]/div[5]/form/micro-ui/scopus-author-profile-page-control-microui/div[1]/div[3]/div/div/div[1]/button[6]/span").click()
time.sleep(randint(2, 4))
number_of_topics = driver.find_element(By.XPATH, "/html/body/div/div/div[1]/div[2]/div/div[3]/div[10]/div[5]/form/micro-ui/scopus-author-profile-page-control-microui/div[1]/div[3]/div/div/div[1]/button[6]/span").text
number_of_topics = number_of_topics[:number_of_topics.find(" ")].replace(",", "")
number_of_topics = int(number_of_topics)
topic_counter = 1
while topic_counter <= number_of_topics:
try:
top = driver.find_element(By.XPATH, "/html/body/div/div/div[1]/div[2]/div/div[3]/div[10]/div[5]/form/micro-ui/scopus-author-profile-page-control-microui/div[1]/div[3]/div/div/div[3]/div[2]/div/div/table/tbody/tr[" + str(topic_counter) + "]/td[1]/button/span").text
docs = driver.find_element(By.XPATH, "/html/body/div/div/div[1]/div[2]/div/div[3]/div[10]/div[5]/form/micro-ui/scopus-author-profile-page-control-microui/div[1]/div[3]/div/div/div[3]/div[2]/div/div/table/tbody/tr[" + str(topic_counter) + "]/td[2]/button/span").text
fwci = driver.find_element(By.XPATH, "/html/body/div/div/div[1]/div[2]/div/div[3]/div[10]/div[5]/form/micro-ui/scopus-author-profile-page-control-microui/div[1]/div[3]/div/div/div[3]/div[2]/div/div/table/tbody/tr[" + str(topic_counter) + "]/td[3]").text
if top == "" or docs == "" or fwci == "":
continue
topics.append(top)
topics_docs.append(docs)
topics_fwci.append(fwci)
topic_counter += 1
except NoSuchElementException as nsee:
topic_counter += 1
continue
except NoSuchElementException as nsee:
pass
topics_nested = []
for i in range(len(topics)):
topics_nested.append({"topic": topics[i], "docs_for_topic": topics_docs[i], "fwci_of_topic": topics_fwci[i]})
# get metrics
collab_internationel_percentage = ""
collab_academic_corporate_percentage = ""
documents_top_25_worldwide_percentage = ""
documents_top_25_journals_percentage = ""
field_weighted_citation_impact = ""
try:
driver.find_element(By.XPATH, "/html/body/div/div/div[1]/div[2]/div/div[3]/div[10]/div[5]/form/micro-ui/scopus-author-profile-page-control-microui/div[1]/div[3]/div/div/div[1]/button[2]/span").click()
time.sleep(randint(2, 4))
try:
collab_internationel_percentage = driver.find_element(By.XPATH, "/html/body/div/div/div[1]/div[2]/div/div[3]/div[10]/div[5]/form/micro-ui/scopus-author-profile-page-control-microui/div[1]/div[3]/div/div/div[3]/div[2]/div/div/div/div[1]/div/div/div[1]/div/div/div/div/div/div[1]/span").text
collab_internationel_percentage = collab_internationel_percentage.replace("%", "")
except NoSuchElementException as nsee:
pass
try:
collab_academic_corporate_percentage = driver.find_element(By.XPATH, "/html/body/div/div/div[1]/div[2]/div/div[3]/div[10]/div[5]/form/micro-ui/scopus-author-profile-page-control-microui/div[1]/div[3]/div/div/div[3]/div[2]/div/div/div/div[1]/div/div/div[2]/div/div/div/div/div[1]/span").text
collab_academic_corporate_percentage = collab_academic_corporate_percentage.replace("%", "")
except NoSuchElementException as nsee:
pass
try:
documents_top_25_worldwide_percentage = driver.find_element(By.XPATH, "/html/body/div/div/div[1]/div[2]/div/div[3]/div[10]/div[5]/form/micro-ui/scopus-author-profile-page-control-microui/div[1]/div[3]/div/div/div[3]/div[2]/div/div/div/div[2]/div/div/div[1]/div/div[1]/div/div/div/div/div[1]/span[1]").text
documents_top_25_worldwide_percentage = documents_top_25_worldwide_percentage.replace("%", "")
except NoSuchElementException as nsee:
pass
try:
documents_top_25_journals_percentage = driver.find_element(By.XPATH, "/html/body/div/div/div[1]/div[2]/div/div[3]/div[10]/div[5]/form/micro-ui/scopus-author-profile-page-control-microui/div[1]/div[3]/div/div/div[3]/div[2]/div/div/div/div[3]/div/div/div[1]/div/div[1]/div/div/div/div/div[1]/span[1]").text
documents_top_25_journals_percentage = documents_top_25_journals_percentage.replace("%", "")
except NoSuchElementException as nsee:
pass
try:
field_weighted_citation_impact = driver.find_element(By.XPATH, "/html/body/div/div/div[1]/div[2]/div/div[3]/div[10]/div[5]/form/micro-ui/scopus-author-profile-page-control-microui/div[1]/div[3]/div/div/div[3]/div[2]/div/div/div/div[4]/div/div/div[1]/div/div[1]/div/div/div/div/div/div[1]/span").text
except NoSuchElementException as nsee:
pass
except NoSuchElementException as nsee:
pass
# build dict
current_scopus_person_data = {}
current_scopus_person_data["lsf_id"] = lsf_id
current_scopus_person_data["url"] = url
current_scopus_person_data["name"] = name
current_scopus_person_data["firstname"] = firstname
current_scopus_person_data["surname"] = surname
current_scopus_person_data["scopus_id"] = scopus_id
current_scopus_person_data["citations"] = citations
current_scopus_person_data["citations_in_docs"] = citations_in_docs
current_scopus_person_data["documents"] = documents
current_scopus_person_data["h_index"] = h_index
current_scopus_person_data["topics"] = topics
current_scopus_person_data["topics_nested"] = topics_nested
current_scopus_person_data["collab_internationel_percentage"] = collab_internationel_percentage
current_scopus_person_data["collab_academic_corporate_percentage"] = collab_academic_corporate_percentage
current_scopus_person_data["documents_top_25_worldwide_percentage"] = documents_top_25_worldwide_percentage
current_scopus_person_data["documents_top_25_journals_percentage"] = documents_top_25_journals_percentage
current_scopus_person_data["field_weighted_citation_impact"] = field_weighted_citation_impact
# upload data
print("- - - - - - Uploading")
es.index(index=index_name, id=lsf_id, document=current_scopus_person_data)
# update log
log["done"].append(l)
except:
print("- - - - - - Error at: " + l)
# write new log
with open(log_name, "w") as outfile:
outfile.write(json.dumps(log))
def check_elastic():
if not es.ping():
sys.exit("No connection to Elasticsearch")
if not es.indices.exists(index=index_name):
settings = {
"analysis": {
"analyzer": {
"searchabilityAnalyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"asciifolding"
]
}
}
}
}
mappings = {
"properties": {
"lsf_id": {
"type": "text",
"analyzer": "searchabilityAnalyzer",
"search_analyzer": "searchabilityAnalyzer",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"url": {
"type": "text",
"analyzer": "searchabilityAnalyzer",
"search_analyzer": "searchabilityAnalyzer",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name": {
"type": "text",
"analyzer": "searchabilityAnalyzer",
"search_analyzer": "searchabilityAnalyzer",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"firstname": {
"type": "text",
"analyzer": "searchabilityAnalyzer",
"search_analyzer": "searchabilityAnalyzer",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"surname": {
"type": "text",
"analyzer": "searchabilityAnalyzer",
"search_analyzer": "searchabilityAnalyzer",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"scopus_id": {
"type": "text",
"analyzer": "searchabilityAnalyzer",
"search_analyzer": "searchabilityAnalyzer",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"citations": {
"type": "text",
"analyzer": "searchabilityAnalyzer",
"search_analyzer": "searchabilityAnalyzer",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"citations_in_docs": {
"type": "text",
"analyzer": "searchabilityAnalyzer",
"search_analyzer": "searchabilityAnalyzer",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"documents": {
"type": "text",
"analyzer": "searchabilityAnalyzer",
"search_analyzer": "searchabilityAnalyzer",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"h_index": {
"type": "text",
"analyzer": "searchabilityAnalyzer",
"search_analyzer": "searchabilityAnalyzer",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"topics": {
"type": "text",
"analyzer": "searchabilityAnalyzer",
"search_analyzer": "searchabilityAnalyzer",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"topics_nested": {
"type": "nested",
"properties": {
"topic": {
"type": "text",
"analyzer": "searchabilityAnalyzer",
"search_analyzer": "searchabilityAnalyzer",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 250
}
}
},
"docs_for_topic": {
"type": "text",
"analyzer": "searchabilityAnalyzer",
"search_analyzer": "searchabilityAnalyzer",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 250
}
}
},
"fwci_of_topic": {
"type": "text",
"analyzer": "searchabilityAnalyzer",
"search_analyzer": "searchabilityAnalyzer",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 250
}
}
}
}
},
"collab_internationel_percentage": {
"type": "text",
"analyzer": "searchabilityAnalyzer",
"search_analyzer": "searchabilityAnalyzer",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"collab_academic_corporate_percentage": {
"type": "text",
"analyzer": "searchabilityAnalyzer",
"search_analyzer": "searchabilityAnalyzer",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"documents_top_25_worldwide_percentage": {
"type": "text",
"analyzer": "searchabilityAnalyzer",
"search_analyzer": "searchabilityAnalyzer",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"documents_top_25_journals_percentage": {
"type": "text",
"analyzer": "searchabilityAnalyzer",
"search_analyzer": "searchabilityAnalyzer",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"field_weighted_citation_impact": {
"type": "text",
"analyzer": "searchabilityAnalyzer",
"search_analyzer": "searchabilityAnalyzer",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
}
}
es.indices.create(index=index_name, mappings=mappings, settings=settings)
def readLinks():
print("Reading in links.")
filename = "scopus_links.json"
# check if json existing
if os.path.exists(filename):
# read in json
with open(filename, 'r') as jsonfile:
links = json.load(jsonfile)
return links["links"]
else:
sys.exit(f"{filename} not found.")
# main - entry point
if __name__ == '__main__':
print("Starting")
check_elastic()
options = Options()
options.add_argument("--headless")
driver = webdriver.Firefox(options=options)
links = readLinks()
getData(driver, links)
driver.close()
print("Finished")