|
| 1 | +#!/usr/bin/env perl |
| 2 | +# Search google with a keyword and get all the anchor links and text in search results. |
| 3 | + |
| 4 | +# https://metacpan.org/pod/LWP::UserAgent |
| 5 | +# https://metacpan.org/pod/HTML::TreeBuilder |
| 6 | + |
| 7 | +use strict; |
| 8 | +use warnings; |
| 9 | +use Carp qw( croak ); |
| 10 | +use LWP::UserAgent (); |
| 11 | +use HTML::TreeBuilder; |
| 12 | + |
| 13 | +sub crawl_results { |
| 14 | + my ($ua, $url) = @_; |
| 15 | + my $response = $ua->get($url); |
| 16 | + |
| 17 | + if ($response->is_success) { |
| 18 | + my $tree = HTML::TreeBuilder->new(); |
| 19 | + $tree->parse($response->decoded_content); |
| 20 | + |
| 21 | + # https://metacpan.org/pod/HTML::Element#look_down |
| 22 | + # We will be getting the 'anchor' tag which is inside the 'div' tag having class 'kCrYT' |
| 23 | + my @anchor_tags = $tree->look_down( |
| 24 | + '_tag' => 'a', |
| 25 | + sub { |
| 26 | + $_[0]->look_up('_tag' => 'div', 'class' => 'kCrYT'); |
| 27 | + } |
| 28 | + ); |
| 29 | + for my $anchor (@anchor_tags) { |
| 30 | + my $anchor_text |
| 31 | + = $anchor->look_down('_tag' => 'div', 'class' => 'BNeawe vvjwJb AP7Wnd'); |
| 32 | + if (defined $anchor_text) { |
| 33 | + print $anchor_text->as_text() . "\n " . $anchor->attr('href') . "\n"; |
| 34 | + } |
| 35 | + else { |
| 36 | + $anchor_text |
| 37 | + = $anchor->look_down('_tag' => 'span', 'class' => 'XLloXe AP7Wnd'); |
| 38 | + print " " . $anchor_text->as_text() . "\n " . $anchor->attr('href') . "\n"; |
| 39 | + } |
| 40 | + |
| 41 | + } |
| 42 | + } |
| 43 | + else { |
| 44 | + croak $response->status_line; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +sub main { |
| 49 | + my $search_text = "Perl"; |
| 50 | + my $url = "https://www.google.com/search?q=" . $search_text; |
| 51 | + |
| 52 | + my $ua = LWP::UserAgent->new(ssl_opts => {verify_hostname => 0}); |
| 53 | + $ua->show_progress(1); |
| 54 | + |
| 55 | + $ua->agent('Mozilla/5.0'); |
| 56 | + |
| 57 | + crawl_results($ua, $url); |
| 58 | +} |
| 59 | + |
| 60 | +main(); |
| 61 | + |
| 62 | + |
| 63 | +__END__ |
| 64 | +
|
| 65 | +Output- |
| 66 | +
|
| 67 | +The Perl Programming Language - www.perl.org |
| 68 | + /url?q=https://www.perl.org/&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQFjAAegQIBRAB&usg=AOvVaw3y3nT09Oz2w6kikznVIuMh |
| 69 | + Perl Download |
| 70 | + /url?q=https://www.perl.org/get.html&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQ0gIwAHoECAUQAg&usg=AOvVaw2gBE2LhG8ygDrly05ICUdw |
| 71 | + About |
| 72 | + /url?q=https://www.perl.org/about.html&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQ0gIwAHoECAUQAw&usg=AOvVaw24DsGYNiDr06YOm3I0ZMZK |
| 73 | + Learn Perl |
| 74 | + /url?q=https://www.perl.org/learn.html&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQ0gIwAHoECAUQBA&usg=AOvVaw3oJC7A0q1L2bBbAuV2vtLH |
| 75 | + Perl Blogs |
| 76 | + /url?q=http://blogs.perl.org/&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQ0gIwAHoECAUQBQ&usg=AOvVaw0NG5PstdaQ-LXgQF1xO527 |
| 77 | + Wikipedia |
| 78 | + /url?q=https://en.wikipedia.org/wiki/Perl&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQmhMwEnoECBUQDg&usg=AOvVaw1Sj3QfRNbZWdksIJMDVpRQ |
| 79 | +Perl.com - programming news, code and culture |
| 80 | + /url?q=https://www.perl.com/&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQFjAcegQIBhAB&usg=AOvVaw1oMYhe_vApCBfMeull0wuW |
| 81 | +Perl - Wikipedia |
| 82 | + /url?q=https://en.wikipedia.org/wiki/Perl&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQFjAdegQICxAB&usg=AOvVaw1hl5JKfzPLO2SiuBF-1JjD |
| 83 | + History |
| 84 | + /url?q=https://en.wikipedia.org/wiki/Perl%23History&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQ0gIwHXoECAsQAg&usg=AOvVaw3TwYV8Wu_P33aqJ3zAfQxJ |
| 85 | + Overview |
| 86 | + /url?q=https://en.wikipedia.org/wiki/Perl%23Overview&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQ0gIwHXoECAsQAw&usg=AOvVaw22fnsL-FL2s9paSxHBL46G |
| 87 | + Database interfaces |
| 88 | + /url?q=https://en.wikipedia.org/wiki/Perl%23Database_interfaces&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQ0gIwHXoECAsQBA&usg=AOvVaw14s-sSUPwFEW6psKWbyUKF |
| 89 | + Larry Wall |
| 90 | + /url?q=https://en.wikipedia.org/wiki/Larry_Wall&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQs2YwHXoECAsQBw&usg=AOvVaw2cTISw6jscTZdSuSwsu0zO |
| 91 | + GNU General Public License |
| 92 | + /url?q=https://en.wikipedia.org/wiki/GNU_General_Public_License&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQs2YwHXoECAsQCQ&usg=AOvVaw22kEPvsqGJ-vhq4kl2ceZs |
| 93 | + Multi-paradigm |
| 94 | + /url?q=https://en.wikipedia.org/wiki/Multi-paradigm&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQs2YwHXoECAsQCw&usg=AOvVaw11J9SG-p_azhwtEbdSSfM4 |
| 95 | + functional |
| 96 | + /url?q=https://en.wikipedia.org/wiki/Functional_programming&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQs2YwHXoECAsQDA&usg=AOvVaw3JtcSXpqWhgQGdra95zHeY |
| 97 | + imperative |
| 98 | + /url?q=https://en.wikipedia.org/wiki/Imperative_programming&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQs2YwHXoECAsQDQ&usg=AOvVaw0sYvOP4Klk4lgYTga4uAtM |
| 99 | + object-oriented |
| 100 | + /url?q=https://en.wikipedia.org/wiki/Object-oriented_programming&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQs2YwHXoECAsQDg&usg=AOvVaw06tbttIjQ5dAgMezRUf4XB |
| 101 | + class-based |
| 102 | + /url?q=https://en.wikipedia.org/wiki/Class-based_programming&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQs2YwHXoECAsQDw&usg=AOvVaw0KRak58-NGipywvgYlBAQX |
| 103 | + reflective |
| 104 | + /url?q=https://en.wikipedia.org/wiki/Reflective_programming&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQs2YwHXoECAsQEA&usg=AOvVaw14Tc5tx4Hy50wDmKq4CgQT |
| 105 | +Perl Tutorial - Tutorialspoint |
| 106 | + /url?q=https://www.tutorialspoint.com/perl/index.htm&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQFjAeegQICBAB&usg=AOvVaw0I4QvcKPi7pfpwWWsU1_B0 |
| 107 | + Perl - Introduction |
| 108 | + /url?q=https://www.tutorialspoint.com/perl/perl_introduction.htm&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQ0gIwHnoECAgQAg&usg=AOvVaw0YWOpaHqZHQA07OLuwe_Tr |
| 109 | + Perl - Arrays |
| 110 | + /url?q=https://www.tutorialspoint.com/perl/perl_arrays.htm&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQ0gIwHnoECAgQAw&usg=AOvVaw0MC522KylwTVJc3RuiqG-f |
| 111 | + Perl - Environment |
| 112 | + /url?q=https://www.tutorialspoint.com/perl/perl_environment.htm&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQ0gIwHnoECAgQBA&usg=AOvVaw2vLcHLWFy4L3sg5Qh6sGmF |
| 113 | + Perl - Variables |
| 114 | + /url?q=https://www.tutorialspoint.com/perl/perl_variables.htm&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQ0gIwHnoECAgQBQ&usg=AOvVaw3oaQXhbe_qQuI_OFvhJ6W4 |
| 115 | +Perl - Introduction - Tutorialspoint |
| 116 | + /url?q=https://www.tutorialspoint.com/perl/perl_introduction.htm&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQFjAfegQIChAB&usg=AOvVaw0rU8BKx99oTDNj6-guNTfa |
| 117 | +Perl Tutorial for Beginners: Learn in 1 Day - Guru99 |
| 118 | + /url?q=https://www.guru99.com/perl-tutorials.html&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQFjAkegQIARAB&usg=AOvVaw2hhKV98I8svSTwlpoMPe6r |
| 119 | +Perl Programming Language - GeeksforGeeks |
| 120 | + /url?q=https://www.geeksforgeeks.org/perl-programming-language/&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQFjAlegQIABAB&usg=AOvVaw3C6q2gA836AhZ34l_evZKL |
| 121 | +Introduction to Perl - GeeksforGeeks |
| 122 | + /url?q=https://www.geeksforgeeks.org/introduction-to-perl/&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQFjAmegQIAxAB&usg=AOvVaw1--1qHvlQqS_SatxPjJwZt |
| 123 | +The Top 10 Programming Tasks That Perl Is Used For | By ActiveState |
| 124 | + /url?q=https://www.activestate.com/blog/top-10-programming-tasks-that-perl-is-used-for/&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQFjAnegQIDBAB&usg=AOvVaw1fO1A3eh2DrU5r0PcjjH_g |
| 125 | +Perl Tutorial |
| 126 | + /url?q=https://www.perltutorial.org/&sa=U&ved=2ahUKEwjnosLT6PXtAhW6yjgGHVOqAlcQFjAoegQIBxAB&usg=AOvVaw0rDSo8UlL65M1D3GGHpNrk |
0 commit comments