Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Passing 2 tags to find() method

by Kaustubh (Acolyte)
on Jul 27, 2016 at 05:26 UTC ( [id://1168612]=perlquestion: print w/replies, xml ) Need Help??

Kaustubh has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I am stuck with the following two questions and I need your guidance to solve it.My question may seem simple or inappropriate but please consider the fact that I am a newbie to Perl.
1)Is it possible to specify 2 html tags in find() method if you are using Mechanize Package?Below is my code snippet which fails to return any output.Basically I have given a URL to get method say for example $mech->get("abc.com/xyz") and I am using this $mech along with find and attr methods.

foreach my $element ($mech->find('span strong')) { print $element->as_text(); }
2)Is there any method available that can take a html tag and an attribute as parameters and return the output instead of using find() and attr() methods separately.
The project that I am working on makes use of Mechanize Package.

Replies are listed 'Best First'.
Re: Passing 2 tags to find() method
by marto (Cardinal) on Jul 27, 2016 at 07:37 UTC
    Can't locate object method "find" via package "WWW::Mechanize" at......

    You claim to be using Mechanize but there is no find method in that module. Are you talking about Mojo::DOM and the code provided in your previous thread?

      Hi Marto,
      I have made use of the following packages:-
      use strict;
      use warnings;
      use Data::Dumper;
      use WWW::Mechanize;
      use WWW::Mechanize::TreeBuilder;
      I think the method is a part of WWW::Mechanize::TreeBuilder.I might have forgotten to mention about it in my previous threads.Sorry for the inconvenience.I have been assigned the task of scrapping the web page.
      The project I am working on is a Web based application that uses Real time Perl WebFramework-> mojolicious.

        "I think the method is a part of WWW::Mechanize::TreeBuilder"

        It isn't. Show the code you're actually running, because nothing provided here does what you claim.

        "The project I am working on is a Web based application that uses Real time Perl WebFramework-> mojolicious."

        The code I gave you uses Mojo::DOM, part of Mojolicious. Previously I asked you to read How do I post a question effectively?. The best way to get help is show an example of the code you are actually running, the input (in this case some HTML), the output you get and the output you want. This approach requires effort from you but is quicker than others trying to guess what you may be doing.

Re: Passing 2 tags to find() method
by marto (Cardinal) on Jul 27, 2016 at 07:53 UTC

    "Is there any method available that can take a html tag and an attribute as parameters and return the output instead of using find() and attr() methods separately. The project that I am working on makes use of Mechanize Package."

    For clarification, show your input (HTML), show the output you expect. I think you may be confusing some of the terms, and modules in use.

Re: Passing 2 tags to find() method
by Kaustubh (Acolyte) on Jul 27, 2016 at 07:01 UTC

    Hi All,
    I have figured out the solution to the first question.
    We can indeed pass 2 html tags to find method when you are using mechanize package.Below is the code snippet to do so:-

    foreach my $element ($mech->find('span','strong')) { my $attr_class=$element->attr('class')|| next; print Dumper($attr_class); }
    Fortunately the above snippet could yield an output.I however could not get an answer to the second question.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-16 19:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found