Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
59
Task/Yahoo--search-interface/D/yahoo--search-interface.d
Normal file
59
Task/Yahoo--search-interface/D/yahoo--search-interface.d
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
import std.stdio, std.exception, std.regex, std.algorithm, std.string,
|
||||
std.net.curl;
|
||||
|
||||
struct YahooResult {
|
||||
string url, title, content;
|
||||
|
||||
string toString() const {
|
||||
return "\nTitle: %s\nLink: %s\nText: %s"
|
||||
.format(title, url, content);
|
||||
}
|
||||
}
|
||||
|
||||
struct YahooSearch {
|
||||
private string query, content;
|
||||
private uint page;
|
||||
|
||||
this(in string query_, in uint page_ = 0) {
|
||||
this.query = query_;
|
||||
this.page = page_;
|
||||
this.content = "http://search.yahoo.com/search?p=%s&b=%d"
|
||||
.format(query, page * 10 + 1).get.assumeUnique;
|
||||
}
|
||||
|
||||
@property results() const {
|
||||
immutable re = `<li>
|
||||
<div \s class="res">
|
||||
<div>
|
||||
<h3>
|
||||
<a \s (?P<linkAttributes> [^>]+)>
|
||||
(?P<linkText> .*?)
|
||||
</a>
|
||||
</h3>
|
||||
</div>
|
||||
<div \s class="abstr">
|
||||
(?P<abstract> .*?)
|
||||
</div>
|
||||
.*?
|
||||
</div>
|
||||
</li>`;
|
||||
|
||||
const clean = (string s) => s.replace("<[^>]*>".regex("g"),"");
|
||||
|
||||
return content.match(re.regex("gx")).map!(m => YahooResult(
|
||||
clean(m.captures["linkAttributes"]
|
||||
.findSplitAfter(`href="`)[1]
|
||||
.findSplitBefore(`"`)[0]),
|
||||
clean(m.captures["linkText"]),
|
||||
clean(m.captures["abstract"])
|
||||
));
|
||||
}
|
||||
|
||||
YahooSearch nextPage() const {
|
||||
return YahooSearch(query, page + 1);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
writefln("%(%s\n%)", "test".YahooSearch.results);
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
#lang racket
|
||||
(require net/url)
|
||||
(define *yaho-url* "http://search.yahoo.com/search?p=~a&b=~a")
|
||||
(define *current-page* 0)
|
||||
(define *current-query* "")
|
||||
(define request (compose port->string get-pure-port string->url))
|
||||
|
||||
;;strip html tags
|
||||
(define (remove-tags text)
|
||||
(regexp-replace* #px"<[^<]+?>" text ""))
|
||||
|
||||
;;search, parse and print
|
||||
(define (search-yahoo query)
|
||||
(unless (string=? *current-query* query) ;different query, go back to page 1
|
||||
(set! *current-query* query)
|
||||
(set! *current-page* 0))
|
||||
(let* ([current-page (number->string (add1 (* 10 *current-page*)))]
|
||||
[html (request (format *yaho-url* query current-page))]
|
||||
[results (regexp-match* #px"lass=\"yschttl spt\" href=\".+?\">(.+?)<span class=url>(.+?)</span>.+?<div class=\"abstr\">(.+?)</div>" html #:match-select cdr)])
|
||||
(for ([result (in-list results)])
|
||||
(printf "Title: ~a \n Link: ~a \n Text: ~a \n\n"
|
||||
(remove-tags (first result))
|
||||
(remove-tags (second result) )
|
||||
(remove-tags (third result))))))
|
||||
|
||||
;;search nexxt page
|
||||
(define (next-page)
|
||||
(set! *current-page* (add1 *current-page*))
|
||||
(search-yahoo *current-query*))
|
||||
Loading…
Add table
Add a link
Reference in a new issue