Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find text based on HTML Comment

How do I use Nokogiri to find text within <!--Sanction 3--> (Parsing the HTML)?

I am entering a search term into the site and the results are displayed on the next page. I need to programmatically grab the data from the results page if it meets certain criteria.

I noticed as I analyzed the results page, that the items are broken up into sanctions. I need to know if the sanction has data and, if so, does it contain my keyword; I am looking for the county/state. I am not sure how to make it look at a sanction. Here is a bit of the HTML:

<!--Sanction 3-->

<table border="2" cellpadding="2" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th class="XXheaderClass" colspan="5" scope="colgroup">
                <table bgcolor="#ff9999" width="100%">
                    <tbody>
                        <tr>
                            <td class="XXsanctionHeader1">
                                <span class="XXtextBold">Requirements Met</span>
                            </td>
                            <td class="XXsanctionHeader2">
                                <span class="XXtextBold">Status: GOOD</span>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </th>
        </tr>
        <tr>
            <th class="XXheaderClass" width="31%" scope="col">
                <span class="XXsmallTextBold">Description</span>
            </th>
            <th class="XXheaderClass" width="12%" scope="col">
                <span class="XXsmallTextBold">Effective Date</span>
            </th>
            <th class="XXheaderClass" width="12%" scope="col">
                <span class="XXsmallTextBold">Number</span>
            </th>
            <th class="XXheaderClass" width="12%" scope="col">
                <span class="XXsmallTextBold">County/State</span>
            </th>
            <th class="XXheaderClass" width="33%" scope="col">
                <span class="XXsmallTextBold">Address and Phone Number</span>
            </th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <td class="footerClass" colspan="5">
                <table class="panelBox">
                    <tr>
                        <td>
                            <a href="SanctionHelpPages/Sanction03Help.aspx" id="MainContent_lvSanction3_sanction03Link" class="outputLinkEx"><span class="XXlinkBold">
                                    Click Here</span></a>
                        </td>
                        <td>
                            <span class="XXtextBold">to resolve,
                                requirements met.</span>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </tfoot>
    <tbody>

        <tr id="MainContent_lvSanction3_Tr1_0">
            <td class="XXsmallText">
                <span id="MainContent_lvSanction3_lblDescription_0">DESCRIPTION     </span>
            </td>
            <td class="XXsmallText">
                <span id="MainContent_lvSanction3_lblEffectiveDate_0">9/19/20011</span>
            </td>
            <td class="XXsmallText">
                <span id="MainContent_lvSanction3_lblNumber_0">1111             </span>
            </td>
            <td class="XXsmallText">
                <span id="MainContent_lvSanction3_lblCountyState_0">MyCounty       </span>
            </td>
            <td class="XXsmallText">
                <span id="MainContent_lvSanction3_lblAddressAndPhoneNumber_0">1234 MyRoad AVE. CITY                                        (xxx)xxx-xxxx</span>
            </td>
        </tr>

    </tbody>
</table>
<br />
like image 808
user1128637 Avatar asked Jul 03 '26 02:07

user1128637


1 Answers

You can use doc.xpath("//comment()") to find all of the comment nodes. You could then iterate through those nodes and check their siblings for your data. Without more information it's somewhat difficult to give a comprehensive answer, though.

like image 78
Chris Heald Avatar answered Jul 04 '26 20:07

Chris Heald