0

I was trying to scrape the data from "non-secured" url that is using 'http' instead of 'https'.
Here is the code

function display_html_info2() {
  $html = file_get_contents('http://adamsonsgroup.com/goldrates/');
  $dom = new DOMDocument();
  $dom->loadHTML($html);
  $xpath = new DOMXPath($dom);
  $h3_element = $xpath->query('/html/body/div[1]/div/div[1]/div[3]/div/div[1]/table/tbody/tr[2]/td[1]/h3')->item(0);
  return $h3_element->nodeValue;
}
add_shortcode('shortcode_name2', 'display_html_info2');

I have also tried using XPath

//*[@id="myCarousel"]/div/div[1]/div[3]/div/div[1]/table/tbody/tr[2]/td[1]/h3

In both the cases, it shows blank output. Means No Value.
This is the tag whose value I am trying to show.
Please let me know how this will work.
I have included the html_dom_parser.php

I tried the above mentioned code but it is giving No Value as Output. Instead, it is showing blank space where is use shortcode [shortcode_name2] to show output of the above code.

  • Additional

I have tried @Pinke Helga method but does not work for me. That's what I did

declare(strict_types = 1);
function display_html_info2() {
  $html = file_get_contents('http://adamsonsgroup.com/goldrates/');

  if (!is_string($html)) {
    return 'Error: Could not retrieve the HTML content.';
  }

  $dom = new DOMDocument();
  $dom->loadHTML($html);
  $xpath = new DOMXPath($dom);
  $h3_element = $xpath->query('//*[@id="myCarousel"]/div/div[1]/div[3]/div/div[1]/table/tr[2]/td[1]/h3')->item(0);
  return $h3_element->nodeValue;
}
echo display_html_info2();
add_shortcode('shortcode_name2', 'display_html_info2');

And that's what I got. "Error: Could not retrieve the HTML content."

1 Answer 1

1

It looks as you have generated the xpath expression from browser dev-tools. The browser extends some HTML. There is no <tbody> in the original source.

Use the xpath expression //*@id="myCarousel"]/div/div[1]/div[3]/div/div[1]/table/tr[2]/td[1]/h3

Complete code:

<?php declare(strict_types = 1);
function display_html_info2() {
  $html = file_get_contents('http://adamsonsgroup.com/goldrates/');
  $dom = new DOMDocument();
  $dom->loadHTML($html);
  $xpath = new DOMXPath($dom);
  $h3_element = $xpath->query('//*[@id="myCarousel"]/div/div[1]/div[3]/div/div[1]/table/tr[2]/td[1]/h3')->item(0);
//  var_dump($h3_element);
  return $h3_element->nodeValue;
}

echo display_html_info2(); // DEBUG output

Current result:

21.898 OMR
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the response @Pinke Helga. But This //*[@id="myCarousel"]/div/div[1]/div[3]/div/div[1]/table/tr[2]/td[1]/h3 too not worked. Still showing blank.
@Alex In my test it worked. Is display_html_info2 called properly from add_shortcode? There might be an issue in your 2nd function. I've added the complete example.
Please check my recent comment.
Please re-read the question. I have added a comment on what you had suggested.
@Alex echo display_html_info2(); is the debug output and not meant to be in the application. There seem to be another issue in your code not provided here. Have you tried my code on its own, independly from the application? If this should not work as a standalone PHP file, you have to check your server configuration. Outgoing requests might be blocked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.