in reply to Parsing Links from .php

It is irrelevant whether or not is PHP. PHP generates HTML and it is the HTML you need to parse. Using Apache's rewrite rules I could generate HTML using perl but make them look like PHP generated output. A certain sort of webmaster might even want to do that to confuse hackers.

WWW::Mechanize may be overkill for your needs and LWP, which the former is built upon, should be adequate. (I think where WWW::Mechanize comes in is where you need to login to a website to see content etc.)

In an ideal world well actually some people think in an ideal world, the web would use YAML or something rather than HTML, but that is a different story...... In an ideal world, all HTML would be valid XHTML. Then you could use XML::LibXML to parse the page and off you go. In practice this is highly unlikely to be the case. You are better off using HTML::TreeBuilder to get you going. Grabbing some code from something similar (but not reuseable) you probably want something like:

require LWP::UserAgent; require HTML::TreeBuilder; my $ua = LWP::UserAgent->new(......); my $response = $ua->get(......); if ($response->is_success) { # get the document from the web my $r = $response->decoded_content; # or whatever my $tidied_doc = HTML::TreeBuilder->new_from_content($r)->as_HTML( +); .................. } else { die $response->status_line; }

The other problem is that if the webpage has any sort of international content it is quite likely to declare itself as being encoded in Latin-1, but to contain a mixture of Latin-1 and unicode characters.

Replies are listed 'Best First'.
Re^2: Parsing Links from .php
by jdetloff (Acolyte) on Jan 11, 2010 at 02:09 UTC

    Thanks for the answer! I actually just got the O'Reilly Perl and LWP book, and I'll be better able to say if this works once I've read through it more, but I have a quick question right off the bat.

    Will this work for a situation where there are several frames, each with a seperate .php file, whose links and data I need to access almost simultaneously?

    The site I'm using has a frame above, to navigate through areas, which are displayed on the frame below.

      Your Iframe HTML will probably look something like:
      <iframe name="FRAME1" src="id77.htm" width="730" height="360" framebor +der="0"></iframe>
      You need to pull out those iframe elements and do another LWP::UserAgent::get on the src attributes. It is a bit like writing a script that reads a webpage and does stuff with the images. However instead of an image you have another round of HTML to parse. I am not sure what you mean by "almost simultaneously".