Dear monks,

I've just worked out a solution for a specific problem I had to solve, and I'd like to share the results.

However, I'm unsure about two things:
My questions:
  1. Should I post/submit this code somewhere ?
  2. If yes: what would be the recommended way to proceed ?
Specifically, I wanted to be able to extract links _and_ the "associated text" - even if that is a fairly vague notion.

So I decided to write the code that, given the following HTML (note the intentionally nested links and somewhat sloppy HTML):
<garbage> <a href="URL1"> text1.0 <img src="SRC1"> text1.1 <br> <garbage2> text1.2 <a href="URL2"> text2.0 <img src="SRC2"> text2.1 <br> <garbage2> <garbage3> text2.2 <a href="URL3"> text3.0 <img src="SRC3"> text3.1 <br> <garbage3> text3.2 <x1 a="a" b="b"> text3.3 </a> text2.3 <haha> </a> text1.3 <oho> <a href="URL4"> text4.0 <img src="SRC4"> text4.1 <br> <garbage4> text4.2 <x1 a="a" b="b"> text4.3 </a> text1.4 </a>
would produce the following (or similar) output:
A URL3 TEXT: ' text3.0 text3.1 text3.2 text3.3 ' A URL2 TEXT: ' text2.0 text2.1 text2.2 text2.3 ' A URL4 TEXT: ' text4.0 text4.1 text4.2 text4.3 ' A URL1 TEXT: ' text1.0 text1.1 text1.2 text1.3 text1.4 '
I did my search homeworks and found a series of good nodes; among them,

Extracting full links from HTML,
Process a HTML file to get information from it.,
Getting to grips with HTML::Parser.

I decided to go for a first attempt with HTML::Parser; I probably will continue and add at least a few of other code variants, as I'm curious to get my own hands-on knowledge about HTML::LinkExtractor and HTML::TokeParser.

The code I've came up so far is on my Scratchpad.

Thank you for any help/suggestions/opinions.

In reply to Parsing HTML - once again by Krambambuli

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.