skip to Main Content

I’m trying to use the PHP Simple DOM Parser to pull information from a website and it isn’t working for me.

Specifically i am trying to get the:

  1. 5-day prediction
  2. 1-month prediction

values from the top of the page.

i am trying to grab the results from this div _ngcontent-coincodex-c2362887433 but not able to extract the data.

no errors encountered. Just not able to get the intended value.

This is my code.

require_once 'simple_html_dom.php';
$url = 'https://coincodex.com/crypto/bad-idea-ai/price-prediction';
$html = file_get_html($url);

foreach($html->find('div._ngcontent-coincodex-c2362887433') as $article) {
    echo 'Output:' .$article;   
}

Grateful if anyone can share with me what I am doing wrong here.

this is the html i m trying to scrape:

<div _ngcontent-coincodex-c2362887433="" placment="top" tooltipclass="tooltip-custom-class" class="prediction-range" aria-describedby="ngb-tooltip-9">
  <div _ngcontent-coincodex-c2362887433="">5-Day Prediction
  </div>
  <div _ngcontent-coincodex-c2362887433="">
    <i _ngcontent-coincodex-c2362887433="" appfontawesome="" class="far fa-long-arrow-down">
    <svg style="pointer-events: none;" aria-hidden="true" focusable="false" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512" class="svg-inline--fa"><path fill="currentColor" d="M300.3 327.5l-19.6-19.6c-4.8-4.8-12.5-4.7-17.1.2L186 388.8V44c0-6.6-5.4-12-12-12h-28c-6.6 0-12 5.4-12 12v344.8l-77.5-80.7c-4.7-4.8-12.4-4.9-17.1-.2l-19.6 19.6c-4.7 4.7-4.7 12.3 0 17l131.8 131.8c4.7 4.7 12.3 4.7 17 0l131.8-131.8c4.6-4.7 4.6-12.3-.1-17z"></path>
    </svg>
    </i> $ 0.0₇3329 <!----><!----><!---->
  </div><!----><!----><!----><!----><!---->
</div>

2

Answers


  1. You can simplify your scraping by looking for the class .prediciton-range, and then grabbing only the elements with unlocked information:

    <?php
    
    /*
    
    Question Author: Blackstone
    Question Answerer: Jacob Mulquin
    Question: Scraping a website using PHP "Simple HTML Dom Parser" for data
    URL: https://stackoverflow.com/questions/77505951/scraping-a-website-using-php-simple-html-dom-parser-for-data
    Tags: , php, web-scraping, web
    
    */
    
    require_once 'simple_html_dom.php';
    
    if (!file_exists('prices.html')) {
      $url = 'https://coincodex.com/crypto/bad-idea-ai/price-prediction';
      $html = file_get_html($url);
      file_put_contents('prices.html', $html);
    } else {
      $html = file_get_html('prices.html');
    }
    
    foreach($html->find('.prediction-range') as $article) {
        $plain_text = $article->plaintext;
        $split = explode('$', $plain_text);
    
        // Ignore "Prediction unlock" entries
        if (count($split) == 1)
          continue;
    
        $prediction_range = $split[0];
        $price = $split[1];
    
        echo 'Output: ' . $prediction_range . ' = ' . $price . PHP_EOL;
    }
    

    Outputs:

    Output: 5-Day Prediction  =  0.0₇3326 
    Output: 1-Month Prediction  =  0.0₇3953
    
    Login or Signup to reply.
  2. Rather than looking for the weird _ngcontent-coincodex-c2362887433, which number may change in the future, it is safer to look for something more static.

    I would recommend looking for the class prediction-range like this:

    require_once 'simple_html_dom.php';
    $url = 'https://coincodex.com/crypto/bad-idea-ai/price-prediction';
    $html = file_get_html($url);
    
    $predictions = $html->find('.prediction-range');
    $fiveDayPrediction = $predictions[0]->children(1)->plaintext;
    $oneMonthPrediction = $predictions[1]->children(1)->plaintext;
    
    echo 'Five day prediction: ' . $fiveDayPrediction . '<br>';
    echo 'One month prediction: ' . $oneMonthPrediction . '<br>';
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search