-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmdpi_htmlpars.py
More file actions
193 lines (160 loc) · 5.58 KB
/
mdpi_htmlpars.py
File metadata and controls
193 lines (160 loc) · 5.58 KB
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
192
193
"""
MDPI html parsing
"""
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
##
if not samples:
multi_flag = False
DIR = "./FULL_DATA/MDPI/"
data_list = []
folders = listdir(DIR)
faults = []
skip_samples = ["A New Open Access Journal",
"Acknowledgment to Reviewers" ,
"Acknowledgement to Reviewers",
"Earth—An Open Access Journal",
"Acknowledgment to the Reviewers",
"Scientific Open Access Journal",
"Book Review",
"Sandra Brown (1944–2017): A Distinguished Tropical Ecologist"]
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')
# Get Title
title = soup.find("h1", {'class': "title hypothesis_container"}).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": "html-abstract"}).find_all(
"div", {"class": "html-p"}
)
])
except AttributeError:
abstract = "no_abstract"
faults.append(f"EXC :: no_abstract: {title}")
# print(abstract)
# Get Content, References and Tables
content_div = soup.find("div", {"class": "html-article-content"})
content_list = []
for con in content_div.find_all("div", {"class": "html-p"}):
if con.find("div", {"class": "html-table-wrap"}):
continue
else:
content_list.append(con)
content = " ".join([divcon.get_text() for divcon in content_list]) # Content
# print(content)
tables = [tab for tab in content_div.find_all("div", {"class": "html-table_show"})] # Tables
# print(tables)
try:
references = [ref.get_text() for ref in content_div.find(
"div", {"class": "html-back"}
).find(
"section", {"id": "html-references_list"}
).find_all(
"li"
)]
except:
references = "no_references"
faults.append(f"EXC :: no_references: {title}")
# print(references)
# Get Authors and Affiliations
authors_div = content_div.find("div", {"class": "art-authors hypothesis_container"}).find_all("span", {"class": "inlineblock"})
authors_and_affiliations = []
authors = []
for a in authors_div:
try:
au = a.find("span").get_text() # Contains Author names
except AttributeError:
au = a.find("div").get_text()
faults.append(f"EXC :: no_author_span: {title}")
nu = a.find("sup").get_text() # Contains affiliations
authors.append(au)
authors_and_affiliations.append((au, nu))
# print(authors_and_affiliations)
# print(authors)
affiliations_div = content_div.find("div", {"class": "art-affiliations"}).find_all("div", {"class": "affiliation"})
affiliations = []
for af in affiliations_div:
affil = af.get_text().strip().split("\n") # Separate number and affiliations text
try:
affiliations.append((affil[0], affil[1]))
except IndexError: # Fix when just
if len(affil) < 2:
affiliations.append(("1", affil[0]))
# print(affiliations)
# Get Keywords
keywords_div = content_div.find("div", {"id": "html-keywords"})
try:
keywords_text = keywords_div.get_text().split(":")
if len(keywords_text) > 1:
keywords = re.split(r"(,|;)", keywords_text[1]) # If "Keywords: "
else:
keywords = re.split(r"(,|;)", keywords_text)
keywords = [k.strip() for k in keywords if len(k) > 1] # Clean newlines, commas, and semicolons
# print(keywords)
except:
keywords = "no_keywords"
faults.append(f"EXC :: no_keywords: {title}")
# Get DOI
doi = content_div.find("div", {"class": "bib-identity"}).get_text()
# print(doi)
# Get Date
date = content_div.find("div", {"class": "pubhistory"}).get_text()
# print(date)
# Structure
paper_data = {
"Title": title,
"Authors_and_Affiliations": authors_and_affiliations,
"Affiliations": affiliations,
"DOI": doi,
"Authors": authors,
"Journal": doi.split()[0],
"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/MDPI/mdpi_({t})_({n}).pickle")
else:
df.to_pickle("./PARS_OUT/test_mdpi.pickle")
print(faults)
##