This repository has been archived by the owner on Nov 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AAAIScraper.py
125 lines (112 loc) · 4.18 KB
/
AAAIScraper.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
from PyPDF4 import PdfFileReader
from bs4 import BeautifulSoup
import urllib
import requests
import io
import csv
import urllib.parse
from Modules import modules
from ResearchPaper import ResearchPaper
class AAAIScraper:
staticmethod
def scrape_aaai(url, year):
research_paper_object_list = []
result = requests.get(url)
src = result.content
print(url)
soup = BeautifulSoup(src, "lxml")
raw_papers_list = soup.find_all("p", class_="left")
for idx, raw_paper in enumerate(raw_papers_list):
authors = raw_paper.find("i").contents[0]
# Title
title = raw_paper.find("a", href=True).text.lstrip()
if title in [
"AAAI Organization",
"Conference Committees",
"Awards",
"Sponsors",
"IAAI-13 Preface",
"AAAI-13 Preface",
"EAAI-13 Preface",
"Invited Talks",
]:
print("skip {}".format(title))
continue
# PDF URL
if year == 2019:
pdf_url = raw_paper.find("a", href=True, text="PDF")["href"]
else:
# TODO commented out somehow...
pdf_url = (
str(raw_paper)
.split("<!-- ")[1]
.split(" -->")[0]
.split('href="')[1]
.split('" class')[0]
)
pdf_url = pdf_url.replace("view", "download")
if "http:" in pdf_url:
pdf_url = pdf_url.replace("http:", "https:")
abstract_url = raw_paper.find("a", href=True)["href"]
if year < 2019:
abstract_url = abstract_url.replace("view", "viewPaper")
abstract_url = abstract_url.replace("http:", "https:")
abs_src = requests.get(abstract_url).content
abs_soup = BeautifulSoup(abs_src, "lxml")
if year == 2019:
abstract = abs_soup.find("p").text.strip()
else:
try:
abstract = (
abs_soup.find("div", id="abstract").find("div").text.strip()
)
except:
print("Failed on abstract {}".format(abstract_url))
# Unique ID: note 2nd to last numeric is the unique ID, unclear what the final numeric is
if year == 2019:
unique_id = pdf_url.split("/")[8]
else:
unique_id = pdf_url.split("/")[9]
path = "./AAAI/" + title + ".pdf"
try:
urllib.request.urlretrieve(pdf_url, path)
# TODO race condition??
# COLNET ":" to "/" issue
fp = open(path, "rb")
paper_text = modules.get_pdf_from_fp(fp)
this_paper = ResearchPaper(
unique_id, year, title, pdf_url, authors, paper_text, abstract
)
research_paper_object_list.append(this_paper)
# some feedback that stuff is happening
print(
"{} / {} {}".format(idx + 1, len(raw_papers_list), this_paper.title)
)
except KeyboardInterrupt:
quit()
except Exception as e:
print("**** PAPER WITH TITLE [{}] FAILED ****".format(title))
print(e)
with open("aaai" + str(year) + ".csv", "w") as new_csv:
writer = csv.writer(new_csv)
header = [
"unique_id",
"year",
"title",
"authors",
"pdf_url",
"paper_text",
"abstract",
]
writer.writerow(header)
for paper in research_paper_object_list:
row = [
paper.unique_id,
paper.year,
paper.title,
paper.authors,
paper.pdf_url,
paper.paper_text,
paper.abstract,
]
writer.writerow(row)