-
Notifications
You must be signed in to change notification settings - Fork 3
/
fe_scraper.py
191 lines (151 loc) · 5.19 KB
/
fe_scraper.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
'''FastEstimator elasticsearch content parsing script. This script download and parse the search content from the fastestimator locally hosted
website.'''
import json
import os
import re
import time
import urllib.request as urllib2
from urllib.parse import urljoin
from bs4 import BeautifulSoup
from selenium import webdriver
#fastestimator url to append
FE_URL = 'https://www.fastestimator.org'
EXAMPLES_DIR = 'examples'
API_DIR = 'api'
TUTORIALS_DIR = 'tutorials'
INSTALL_DIR = 'install'
MAIN_DIR = 'main'
#change this to stage env for crawling in pipeline
LOCAL_URL = 'http://localhost:4200'
#initialize the selenium driver for the chrome
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--headless')
driver = webdriver.Chrome(
executable_path=
"D:\\FastEstimator\\chromedriver_win32\\chromedriver.exe",
chrome_options=options)
def clean_body(text):
s = re.sub(r"\s\s+", " ", text)
s = s.replace('\n', ' ')
return s
def save_json_file(fname, parent_dir, item):
#create directory if it doesnt exsit and save file
if not os.path.exists(parent_dir):
os.makedirs(parent_dir)
with open(os.path.join(parent_dir, fname), 'w') as f:
f.write(json.dumps(item))
def extract_examples(url):
links = []
driver.get(url)
page_source = driver.page_source
soup = BeautifulSoup(page_source, 'lxml')
nodes = soup.find_all('mat-tree-node')
for node in nodes:
a = node.find('a')
links.append(a.attrs['href'])
for link in links:
item = {}
driver.get(LOCAL_URL + link)
soup = BeautifulSoup(driver.page_source, 'lxml')
markdown = soup.find('markdown')
h1 = soup.find('h1')
item['link'] = FE_URL + link
item['body'] = clean_body(markdown.text)
item['title'] = h1.text
#save json file
fname = item['link'].split('/')[-1] + '.json'
save_json_file(fname, EXAMPLES_DIR, item)
def extract_tutorial(url):
links = []
driver.get(url)
page_source = driver.page_source
soup = BeautifulSoup(page_source, 'lxml')
nodes = soup.find_all('mat-tree-node')
for node in nodes:
a = node.find('a')
links.append(a.attrs['href'])
print(links)
for link in links:
item = {}
driver.get(LOCAL_URL + link)
soup = BeautifulSoup(driver.page_source, 'lxml')
markdown = soup.find('markdown')
h1 = soup.find('h1')
item['link'] = FE_URL + link
item['body'] = clean_body(markdown.text)
title = h1.text
item['title'] = title
#save json file
fname = item['link'].split('/')[-1] + '.json'
save_json_file(fname, TUTORIALS_DIR, item)
def extract_api(url):
links = []
driver.get(url)
page_source = driver.page_source
soup = BeautifulSoup(page_source, 'lxml')
nodes = soup.find_all('mat-tree-node')
for node in nodes:
a = node.find('a')
links.append(a.attrs['href'])
print(links)
for link in links:
item = {}
driver.get(LOCAL_URL + link)
soup = BeautifulSoup(driver.page_source, 'lxml')
markdown = soup.find('markdown')
item['link'] = FE_URL + link
item['body'] = clean_body(markdown.text)
title = item['link'].split('/')[-1]
dirname = item['link'].split('/')[-2]
item['title'] = title
#save json file
fname = title + '.json'
save_json_file(dirname+'_'+fname, API_DIR, item)
def extract_install(url):
driver.get(url)
item = {}
soup = BeautifulSoup(driver.page_source, 'lxml')
div = soup.find('div', {'class': 'content'})
item['link'] = FE_URL + '/install'
item['body'] = clean_body(div.text)
item['title'] = 'Install'
fname = item['link'].split('/')[-1] + '.json'
save_json_file(fname, INSTALL_DIR, item)
def extract_main(url):
driver.get(url)
item = {}
soup = BeautifulSoup(driver.page_source, 'lxml')
div = soup.find('div', {'class': 'container'})
item['link'] = FE_URL
item['body'] = clean_body(div.text)
item['title'] = 'Getting Started'
fname = 'gettingstarted.json'
save_json_file(fname, MAIN_DIR, item)
'''
FUTURE SETTING: Initial point to follow outbound anchor tags in multiple depths and crawl
HTML pages on the way. Addtionally, crawler needs to identify specific page and store it in the
directory.
'''
def extract_main_list(url):
driver.get(url)
soup = BeautifulSoup(driver.page_source, 'lxml')
ul = soup.find_all('li', {'class': 'nav-item'})
for li in ul:
a = li.find('a')
if __name__ == '__main__':
#relative urls
example_rel_url = 'examples/overview'
tutorial_rel_url = 'tutorials/beginner/t01_getting_started'
api_rel_url = 'api/fe/Estimator'
install_rel_url = 'install'
example_url = urljoin(LOCAL_URL, example_rel_url)
tutorial_url = urljoin(LOCAL_URL, tutorial_rel_url)
api_url = urljoin(LOCAL_URL, api_rel_url)
install_url = urljoin(LOCAL_URL, install_rel_url)
extract_examples(example_url)
extract_tutorial(tutorial_url)
extract_api(api_url)
extract_install(install_url)
extract_main(LOCAL_URL)
#extract_main_list(main_url)