-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecoapp_htmlpars.py
191 lines (155 loc) · 5.04 KB
/
ecoapp_htmlpars.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""
EcoApp HTML pars
"""
from bs4 import BeautifulSoup
import re
from os import listdir, mkdir
import pandas as pd
from tqdm import tqdm
import logging
from parser_html import *
import requests
from functions import *
import argparse
from time import time
from random import randint
## Multprocessing add-on
def list_of_strings(arg):
return arg.split('žž')
def number(arg):
return arg
parser = argparse.ArgumentParser()
parser.add_argument("--str-list", type=list_of_strings)
args = parser.parse_args()
samples = args.str_list
multi_flag = True # Flag to see if script is run on multiprocessing manner
##
DIR = "./FULL_DATA/ECOAPP/"
##
logging.basicConfig(
format='%(asctime)s %(message)s',
filename="_".join(DIR.split("/")),
filemode='w',
) # Adds time to warning output
data_list = []
faults = []
skip_samples = []
# Checking if loaded via multiprocessing
if not samples:
samples = listdir(DIR)
for sample in samples:
# print("\n")
print(sample)
# Read from html file
with open(DIR + sample, "r") as f:
html = f.read()
# Load as soup object
soup = BeautifulSoup(html, 'html.parser')
# print(soup)
# Get Title
title = soup.find("h1", {'class': "citation__title"}).get_text().strip()
print(title)
# Skip samples
if any(skip in title for skip in skip_samples):
continue
# Get Abstract
try:
abstract = " ".join(
[abs.get_text() for abs in soup.find("section", {"class": "article-section__abstract"}).find_all(
"div"
)
])
except AttributeError:
abstract = "no_abstract"
faults.append(f"EXC :: no_abstract: {title}")
# print("\n", abstract)
# Get Content, References and Tables
paper_div = soup.find("article") # Whole content with authors etc
article_div = paper_div.find("article") # Ppaer textual content, Abstract etc
# print(article_div)
content_list = []
try:
for con in article_div.find_all("p"):
content_list.append(con)
content = " ".join([divcon.get_text() for divcon in content_list]) # Content
except AttributeError:
content = "no_content"
faults.append(f"EXC :: no_content: {title}")
# print(content)
# tables = [tab for tab in article_div.find_all("div", {"class": "article-table-content"})] # Tables
# print(tables)
references = "no_references"
# print(references)
# Get Authors and Affiliations
authors_div = paper_div.find("div", {"class": "citation"}).find_all("div", {"class": "author-info accordion-tabbed__content"})
# print(authors_div)
# For output
authors_and_affiliations = []
authors = []
affiliations = []
# For midsteps
authors_affil_dict = {}
affils_list = []
# Authors
for a in authors_div:
author = a.find("p", {"class": "author-name"}).get_text()
affils = [affil.get_text() for affil in a.find_all("p") if affil.get_text() != author]
# print(author)
affils_list.extend(affils) # Get all affiliations per author
authors_affil_dict[author] = affils
authors.append(author) # Add to authors
affils_list = list(set(affils_list)) # Clean duplicates
# Affiliations
for i, af in enumerate(affils_list):
affiliations.append((i+1, af))
# Authors and affiliations
for aaf in authors_affil_dict:
numbers = []
for aff in authors_affil_dict[aaf]:
numbers.append(str(affils_list.index(aff) + 1))
affiliation_numbers = ", ".join(numbers)
authors_and_affiliations.append((aaf, affiliation_numbers))
# print(authors_and_affiliations, authors, affiliations)
# Get Keywords
keywords = []
keywords_meta = soup.find_all("meta", {"name": "citation_keywords"})
# print(keywords_div)
try:
for k in keywords_meta:
keywords.append(k["content"])
except AttributeError:
keywords = "no_keywords"
faults.append(f"EXC :: no_keywords: {title}")
# Get DOI
doi = soup.find("meta", {"property": "og:url"})["content"].split("/doi")[1]
# print(doi)
# Get Date
date = soup.find("span", {"class": "epub-date"}).get_text()
# print(date)
# Structure
paper_data = {
"Title": title,
"Authors_and_Affiliations": authors_and_affiliations,
"Affiliations": affiliations,
"DOI": [doi],
"Authors": authors,
"Journal": doi,
"Date": date,
"Subjects": "no_subjects",
"Abstract": abstract,
"References": references,
"Content": content,
"Keywords": keywords,
"Style": "html",
}
data_list.append(paper_data)
##
t = round(time(), 1) # Timestamp when multiprocessing
n = randint(1, 10) # For fragments of dataframes
df = pd.DataFrame(data_list)
if multi_flag:
df.to_pickle(f"./RESULTS/ECOAPP/ecoapp_({t})_({n}).pickle")
else:
df.to_pickle("./PARS_OUT/test_ecoapp.pickle")
print(faults)
##