|
@@ -1,30 +1,58 @@
|
|
1
|
+## Stackoverflow (It)
|
|
2
|
+#
|
|
3
|
+# @website https://stackoverflow.com/
|
|
4
|
+# @provide-api not clear (https://api.stackexchange.com/docs/advanced-search)
|
|
5
|
+#
|
|
6
|
+# @using-api no
|
|
7
|
+# @results HTML
|
|
8
|
+# @stable no (HTML can change)
|
|
9
|
+# @parse url, title, content
|
|
10
|
+
|
1
|
11
|
from urlparse import urljoin
|
2
|
12
|
from cgi import escape
|
3
|
13
|
from urllib import urlencode
|
4
|
14
|
from lxml import html
|
5
|
15
|
|
|
16
|
+# engine dependent config
|
6
|
17
|
categories = ['it']
|
|
18
|
+paging = True
|
7
|
19
|
|
|
20
|
+# search-url
|
8
|
21
|
url = 'http://stackoverflow.com/'
|
9
|
22
|
search_url = url+'search?{query}&page={pageno}'
|
10
|
|
-result_xpath = './/div[@class="excerpt"]//text()'
|
11
|
23
|
|
12
|
|
-paging = True
|
|
24
|
+# specific xpath variables
|
|
25
|
+results_xpath = '//div[contains(@class,"question-summary")]'
|
|
26
|
+link_xpath = './/div[@class="result-link"]//a|.//div[@class="summary"]//h3//a'
|
|
27
|
+title_xpath = './/text()'
|
|
28
|
+content_xpath = './/div[@class="excerpt"]//text()'
|
13
|
29
|
|
14
|
30
|
|
|
31
|
+# do search-request
|
15
|
32
|
def request(query, params):
|
16
|
33
|
params['url'] = search_url.format(query=urlencode({'q': query}),
|
17
|
34
|
pageno=params['pageno'])
|
|
35
|
+
|
18
|
36
|
return params
|
19
|
37
|
|
20
|
38
|
|
|
39
|
+# get response from search-request
|
21
|
40
|
def response(resp):
|
22
|
41
|
results = []
|
|
42
|
+
|
23
|
43
|
dom = html.fromstring(resp.text)
|
24
|
|
- for result in dom.xpath('//div[@class="question-summary search-result"]'):
|
25
|
|
- link = result.xpath('.//div[@class="result-link"]//a')[0]
|
|
44
|
+
|
|
45
|
+ # parse results
|
|
46
|
+ for result in dom.xpath(results_xpath):
|
|
47
|
+ link = result.xpath(link_xpath)[0]
|
26
|
48
|
href = urljoin(url, link.attrib.get('href'))
|
27
|
|
- title = escape(' '.join(link.xpath('.//text()')))
|
28
|
|
- content = escape(' '.join(result.xpath(result_xpath)))
|
29
|
|
- results.append({'url': href, 'title': title, 'content': content})
|
|
49
|
+ title = escape(' '.join(link.xpath(title_xpath)))
|
|
50
|
+ content = escape(' '.join(result.xpath(content_xpath)))
|
|
51
|
+
|
|
52
|
+ # append result
|
|
53
|
+ results.append({'url': href,
|
|
54
|
+ 'title': title,
|
|
55
|
+ 'content': content})
|
|
56
|
+
|
|
57
|
+ # return results
|
30
|
58
|
return results
|