Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7929493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:05:46+00:00 2026-06-03T20:05:46+00:00

I wrote some codes in PHP to extract content of some elements from other

  • 0

I wrote some codes in PHP to extract content of some elements from other websites. These elements are addressed by XPath. These codes worked for one website successfully but failed for the other one. Therefore, I am sure that not the whole code is incorrect.

by the way: I extracted the element’s XPath address by using ‘Inspect Element’ in Firefox and Right Click on the element and choosing ‘Copy XPath’.

What is wrong for the second website?

thanks

Here is the code:

//MyCode.PHP
<html>
<head>
<title>This is the title</title>
</head>
<body>

<?php 

class EmDIV
{
    public $url="";
    public $content="";
    public $name="";
    public $query="";
    public function EmDIV($CdivName,$Curl,$CQuery)
    {
        $this->name=$CdivName;
        $this->url=$Curl;
        $this->query=$CQuery;
        $html = new DOMDocument();
        @$html->loadHtmlFile($this->url);

        $bodies = $html->getElementsByTagName('body');
        assert($bodies->length === 1);
        $body = $bodies->item(0);
        $xpath = new DOMXPath( $html );
        $nodelist = $xpath->query($this->query);
        //echo @$body->saveHTML();
        if($nodelist->length==1)
        {
            $this->content=$nodelist->item(0)->textContent;
            //sanitizing
            //$this->content=Jsoup.clean($this->content, Whitelist.basic());
        }
        //echo $nodelist->item(0)->nodeName;        
        foreach ($nodelist as $node) 
            echo $node->getNodePath()."\n";     
    }
}

$emdiv=array(
//new EmDIV('parsmalaysia','http://www.parsmalaysia.com/exchange.html','/html/body/div/div[5]/div/div/div/div/div/div/div/div/div/div/table/tbody/tr/td[4]/text()'),
new EmDIV('atlas-exchange','http://atlas-exchange.com/','/html/body/div/div/table/tr/td/table/tr/td/div/div[10]/div/div/div/table/tr[3]/td[2]/text()'),
new EmDIV('usunmalaysia','http://www.usunmalaysia.com/Home.aspx','/html/body/form/table/tbody/tr[3]/td/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr[4]/td/table/tbody/tr/td/table/tbody/tr/td[2]/div/table/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr[2]/td/table/tbody/tr[3]/td/table/tbody/tr[4]/td[2]/text()'),
);
?>

<table border="1">
<tr>
<td>Site</td>
<td>RM Price</td>
</tr>
    <?php
        foreach ($emdiv as $ed) 
        {
            echo "<tr>";
            echo "<td>".$ed->name."</td>";
            echo "<td>".$ed->content."</td>";
            echo "</tr>";
        }
    ?>
</table> 

</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:05:47+00:00Added an answer on June 3, 2026 at 8:05 pm

    In the second page there are no tbody elements. You have to remove all tbody elementes from the path. Firefox show the tbody elements because they are part of the internal structure of the tables, but they are not in the markup you are retrieving.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this PHP code to make some substitutions: function cambio($txt){ $from=array( '/\+\>([^\+\>]+)\<\+/', //finds
I wrote some code to send an email from my PHP script using PHPMailer.
I wrote some codes so as to send e mail but I can only
I wrote some ajax code that sends values to a php file for validation
I wrote some PHP code that will allow me to create an html dropdown
I wrote some MooTools code that reads from YouTube's API in JSON and I
In my web application i've got an iframe from different domain. I wrote some
I have some PHP code I'm moving from a development directory to the webserver
We wrote some code involving usort which works fine on our development systems (PHP
I'm getting some odd results where 2 identical functions (one in PHP and one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.