Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

22
LINES

< > BotCompany Repo | #1011241 // quickGoogle - returns pairs of (link, text)

JavaX fragment (include)

1  
static L<Pair<S>> quickGoogle(S query) {
2  
  new L<Pair<S>> out;
3  
  
4  
  S userAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0";
5  
  S html = loadPageWithUserAgent("http://google.com/search?q=" + urlencode(query) + "&lr=lang_en&hl=en", userAgent);
6  
  S url = first(loadPage_responseHeaders->get("Location"));
7  
  if (url != null)
8  
    html = loadPageWithUserAgent(url, userAgent);
9  
    
10  
  LL<S> h3s = findContainerTagDeep(html, "h3");
11  
  for (L<S> tok : h3s) {
12  
    L<S> linkTok = first(findContainerTag(tok, "a"));
13  
    if (empty(linkTok)) continue;
14  
    
15  
    S link = tagGet(second(linkTok), "href");
16  
    continue unless isAbsoluteURL(link);
17  
    S text = htmldecode(join(dropTags(contentsOfContainerTag(linkTok))));
18  
    out.add(pair(link, text));
19  
  }
20  
  
21  
  ret out;
22  
}

Author comment

Began life as a copy of #1011236

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, xrpafgyirdlv

No comments. add comment

Snippet ID: #1011241
Snippet name: quickGoogle - returns pairs of (link, text)
Eternal ID of this version: #1011241/4
Text MD5: 04086e6adb61b4a7974a69cc157d216f
Author: stefan
Category: javax / networking
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-11-14 09:32:13
Source code size: 812 bytes / 22 lines
Pitched / IR pitched: No / No
Views / Downloads: 441 / 455
Version history: 3 change(s)
Referenced in: [show references]