Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: searching with mojo dom

by marto (Cardinal)
on Mar 11, 2018 at 08:23 UTC ( [id://1210644]=note: print w/replies, xml ) Need Help??


in reply to searching with mojo dom

If the hero div is always the same number can't you just use the nth-child selector, in the same way you do for the table row? Failing that a short example of the HTML would help provide a solution.

Replies are listed 'Best First'.
Re^2: searching with mojo dom
by adambot (Acolyte) on Mar 11, 2018 at 23:56 UTC

    Here is my final code that works. If anyone has a better way to do it, please share.

    my $dom = Mojo::DOM->new($source); my $sidecarsource = $dom->find('div.today_nowcard-sidecar')->first(qr/ +Right Now/)->content; #print Data::Dumper::Dumper($sidecarsource); my $sidecardom = Mojo::DOM->new($sidecarsource); my $wind = $sidecardom->at('table > tbody > tr:nth-child(1) > td > spa +n')->text;

      You might find this cleaner:

      #!/usr/bin/perl use strict; use warnings; use feature 'say'; use Mojo::UserAgent; my $url = 'https://weather.com/weather/today/l/20001:4:US'; my $selector = 'div.today_nowcard-sidecar.component.panel table tr td +span'; my $ua = Mojo::UserAgent->new; say $ua->get( $url )->res->dom->at( $selector )->all_text;

      However, you may want to check if their recommended API, if it offers what you want it'll be faster to access and your code more resilient to changes.

Re^2: searching with mojo dom
by adambot (Acolyte) on Mar 11, 2018 at 15:49 UTC

    the HTML i'm working with can be found here: https://weather.com/weather/today/l/20001:4:US

    the full/unedited selector is:

    #APP > div > div.today.section-local-suite.page > div.section-page-nam +e > div.hero.hero-background.layout-centered > div.hero-flex.styles-d +2KKDEYo__heroFlex__3UOm0 > div.region.region-hero-left > div > sectio +n > div.today_nowcard-sidecar.component.panel > table > tbody > tr:nt +h-child(1) > td > span
    (the wind speed text)
      I'm thinking if i could figure out how to do a div find that would be easiest -- i have my @headers = $dom->find('div ~ today_nowcard-sidecar')->map('text')->each; but it doesn't seem to find anything...

        working more on this, Here is the search code that works:

        @data = $dom->find('div.today_nowcard-sidecar')->each;

        Now i'm left trying to dig through all the data -- when i go through the array i find that the info i need is represented in the dom identified by ${$VAR1}->{'tree'}->[4]->[5]->[4]->[5]->[4] (data gotten using Data::Dumper on @data). Any pointers?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1210644]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (None)
    As of 2024-04-25 01:43 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found