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)

static L<Pair<S>> quickGoogle(S query) {
  new L<Pair<S>> out;
  
  S userAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0";
  S html = loadPageWithUserAgent("http://google.com/search?q=" + urlencode(query) + "&lr=lang_en&hl=en", userAgent);
  S url = first(loadPage_responseHeaders->get("Location"));
  if (url != null)
    html = loadPageWithUserAgent(url, userAgent);
    
  LL<S> h3s = findContainerTagDeep(html, "h3");
  for (L<S> tok : h3s) {
    L<S> linkTok = first(findContainerTag(tok, "a"));
    if (empty(linkTok)) continue;
    
    S link = tagGet(second(linkTok), "href");
    continue unless isAbsoluteURL(link);
    S text = htmldecode(join(dropTags(contentsOfContainerTag(linkTok))));
    out.add(pair(link, text));
  }
  
  ret out;
}

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: 511 / 526
Version history: 3 change(s)
Referenced in: [show references]