-
Notifications
You must be signed in to change notification settings - Fork 0
/
sep_search_result.py
56 lines (49 loc) · 1.69 KB
/
sep_search_result.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
from lxml import html
from helpers.stop_words import StopWordFilter
import re
import requests
from constants import SEP_URL
class SEPSearchResult():
query = None
results = None
def __init__(self, query):
self.set_query(query)
def set_query(self, query):
# query_no_accents = remove_accents(query)
query_no_posessives = re.sub("'s", '', query)
pattern = re.compile('[^a-zA-Z\d\s]')
stripped_query = re.sub(pattern, ' ', query_no_posessives)
stop_word_filter = StopWordFilter()
self.query = stop_word_filter.filter(str(stripped_query).lower().split())
@property
def url(self):
url = SEP_URL + "search/searcher.py?query="
for word in self.query:
url += word + "+"
print url
return url
def request_results(self):
page = requests.get(self.url)
# Remvoe bold tags
text_no_bold = re.sub('</? ?b>', '', page.text)
text_no_newlines = re.sub('\n', '', text_no_bold)
tree = html.fromstring(text_no_newlines.encode('utf-8'))
titles = tree.xpath("//div[@class='result_title']/a/text()")
urls = tree.xpath("//div[@class='result_title']/a/@href")
# Figure out how many results to return
result_length = 0
if len(titles) > 5:
result_length = 5
else:
result_length = len(titles)
# Build the output tuples
output = []
for i in range(result_length):
output.append(
{
"title": titles[i],
"url": SEP_URL + urls[i].lstrip("../")
}
)
self.results = output
return output