Search code examples
phpweb-scrapingdomdocumentdomxpath

Scraping Webpage with DOMdocument and DOMXpath


I'm very new to this. I would like to extract a table from a page using PHP and return it's HTML after modifying the HREF values of all anchors. Here is the table:

    <html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1255">
    <link rel="stylesheet" type="text/css" href="../CssGraduateE.css">
    <title></title>
</head>
<body>
    <div>
        <br>
        <table class="main" cellspacing="0" cellpadding="0">
            <tbody>
                <tr>
                    <td>
                        <br><span class="MainHeader">Subjects in Faculty -   Electrical Engineering</span><br><br>
                        <table cellpadding="2" cellspacing="0" border="1" width="100%">
                            <tbody>
                                <tr>
                                    <td><span class="SecondHeader"> Subject Number</span></td>
                                    <td><span class="SecondHeader">Subject   Name</span></td>
                                    <td><span class="SecondHeader">Points</span></td>
                                    <td><span class="SecondHeader">Semesters</span></td>
                                    <td>Subject Site</td>
                                </tr>
                                <tr>
                                    <td><a href="../Subjects/?SUB=46001">46001</a>&nbsp;</td>
                                    <td nowrap="">Engineering of Distributed Software Sys</td>
                                    <td>3</td>
                                    <td><br></td>
                                    <td><a target="_newtab" href="http://www.thislinkisok.com/courses/046001">www</a></td>
                                </tr>
                                <tr>
                                    <td><a href="../Subjects/?SUB=46002">46002</a>&nbsp;</td>
                                    <td nowrap="">Design and Analysis of Algorithms</td>
                                    <td>3</td>
                                    <td>B<br></td>
                                    <td>&nbsp;<br></td>
                                </tr>
                            </tbody>
                        </table>
                    </td>
                </tr>
            </tbody>
        </table>
        <br>
        <table border="0">
            <tbody>
                <tr>
                    <td>Last Update on :</td>
                    <td>Wednesday ,9 April 2014</td>
                    <td></td>
                </tr>
            </tbody>
        </table>
    </div>
</body>
</html>    

I know how to grab the table I want: $query = $xpath->query('//table[@class="main"]//table[1]'); but how do I loop through all the links that begin with "../xxx" and modify them to something like this: "www.mynewlink.com/xxx" ? At the end I would like to return the extracted table as HTML. How do I do this with native DOMDocument and DOMXpath?

Thanks All!


Solution

  • If $html is your string with HTML you get from the external website, you can do something like this:

    $dom = new DOMDocument();
    @$dom->loadHTML($html);
    
    $xpath = new DOMXPath($dom);
    
    foreach($xpath->query('//table[@class="main"]//a[starts-with(@href, "../")]') as $link) {
        $link->setAttribute('href', preg_replace('#^..#', 'http://www.mynewlink.com', $link->getAttribute('href')));
    }
    
    $container = new DOMDocument();
    $container->appendChild($container->importNode($xpath->query('//table[@class="main"]')->item(0), true));
    
    echo $container->saveHTML();