-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_terms.py
71 lines (54 loc) · 1.99 KB
/
get_terms.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
from bs4 import BeautifulSoup
import requests
url_base = "https://en.wikipedia.org"
category_base_url = "/wiki/Category:"
def create_url(site_url, category_extension, category):
return site_url + category_extension + category
def get_final_terms(category, depth):
final_terms = []
root_category_url = create_url(url_base, category_base_url, category.replace(" ", "_"))
root_terms, root_fetched_urls = scan_url(root_category_url)
last_lvl_urls = root_fetched_urls
for term in root_terms:
final_terms.append(term)
current_lvl_urls = []
for i in range(depth):
for url in last_lvl_urls:
lvl_terms, lvl_fetched_urls = scan_url(url)
for term in lvl_terms:
final_terms.append(term)
for lvl_url in lvl_fetched_urls:
current_lvl_urls.append(lvl_url)
last_lvl_urls = current_lvl_urls
return set(final_terms)
def scan_url(url):
terms = []
categories_url = []
log("fetching " + url)
data = requests.get(url).text
log("fetch " + url + " completed")
soup = BeautifulSoup(data)
for term in get_terms_from_soup(soup):
terms.append(term)
for category_url in get_categories_url(soup):
categories_url.append(url_base + category_url)
return terms, categories_url
def get_terms_from_soup(soup):
possible_selectors = [".mw-category a"]
terms = []
for possible_selector in possible_selectors:
terms_items = soup.select(possible_selector);
for term in terms_items:
if term is not None:
terms.append(term.text)
return terms
def get_categories_url(soup):
possible_selectors = ["#mw-subcategories a.CategoryTreeLabel"]
categories_url = []
for possible_selector in possible_selectors:
for category in soup.select(possible_selector):
categories_url.append(category.attrs["href"])
return categories_url;
def log(msg):
print msg
#print get_final_terms("web development", 1)