Skip to content

Instantly share code, notes, and snippets.

@Xliff
Created November 10, 2019 16:35
Show Gist options
  • Save Xliff/5fb6df9f353c812502ece6bf15e59755 to your computer and use it in GitHub Desktop.
Save Xliff/5fb6df9f353c812502ece6bf15e59755 to your computer and use it in GitHub Desktop.
Yahoo! search interface

I was working on the Yahoo search interface problem at Rosetta Code, and came up with the following:

use Gumbo; 
use LWP::Simple; 
my $dom = parse-html( LWP::Simple.get("http://search.yahoo.com/search?p=test") ); 
my @r = $dom.lookfor( TAG => "h3", class => "title"); 
for @r { 
  .lookfor( TAG => "a" ).map({ [ .attribs<href>, .contents[0] ] }).say; 
  my $desc = .parent.parent.lookfor( TAG => "div" )[1]; 
  $desc.nodes.map( *.contents ).say 
}; 
$dom.lookfor( TAG => "a", class => "next").say

It's mostly straight-forward until I try to pull the text out of the node in $desc...then it becomes problematic.

I've noticed that a lot of things are a bit wonky with Perl6's XML module, and the author looks to be fairly inactive. Unfortunately, Gumbo is tied to it. XML really could use a text method. I will see if I can write one...

@Xliff
Copy link
Author

Xliff commented Nov 10, 2019

Could cut down MAIN to:

sub MAIN (Str $search-term) {
  YahooSearch.new($search-term).results.next.results;
}

If I return self from results...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment