Home > Mobile >  Can anyone help me by getting The plain text from a div placed in an html file
Can anyone help me by getting The plain text from a div placed in an html file

Time:10-08

i am a facing an error while using php simple html dom . I am working on my school project to get the plain text from a following div which is situated in an html file

<div class="product-detail__TextPrice-sc-1k47nh4-0 hECROB d-flex align-items-center  justify-content-md-start">
  <div class="mb-0 weight-bold price"><!-- --> <!-- -->126.00
  </div>
  <div class="mrp ml-md-4 ml-lg-2 pd-mrp">
    ₹ 150.00
  </div>
  <div class=" w-50 margin-text ml-md-4 ml-lg-2">
    <span class="main-slab main-slab-detail">
      1 pc
    </span>
    Margin ₹ 24.00 | 16.00%
  </div>
</div>

This div is present in an html file and we have got only the url to file and we have to first bring the mrp cost of item in div class mrp ml-md-4 ml-lg-2 pd-mrp and cost from class mb-0 weight-bold price I was using php simple html dom to do the process but i am getting a huge array and I can't figure out what to do now . Can anyone please guide me out ,. The code which i am using is below

<?php
include('simple_html_dom.php'); 

$cl=file_get_contents($url);

$html=new simple_html_dom();
$html->load($cl);
$ret = $html->find('div[class=mrp ml-md-4 ml-lg-2 pd-mrp]');

var_dump($ret);

?>

Thanks in advance

CodePudding user response:

Try this script.

Because $html->find('div.pd-mrp') return multiple elements.

$html=new simple_html_dom();
$html->load($cl);

foreach($html->find('div.pd-mrp') as $elements) {
    echo $elements->plaintext;
    break;
}

CodePudding user response:

Final answer :

The target value is a price on a product page. This value is not served at first call, so simple_html_dom can't access it. The workaround would be to scrap the frontend page in a browser extension for example, and to get the value via javascript. This way, you could get your value and process it or send it back to php, if you need.

First answer :

I've made a text text on my server :

<?php

include('simple_html_dom.php'); 

$cl = <<< TEST
<div class="product-detail__TextPrice-sc-1k47nh4-0 hECROB d-flex align-items-center  justify-content-md-start">
  <div class="mb-0 weight-bold price"><!-- --> <!-- -->126.00
  </div>
  <div class="mrp ml-md-4 ml-lg-2 pd-mrp">
    ₹ 150.00
  </div>
  <div class=" w-50 margin-text ml-md-4 ml-lg-2">
    <span class="main-slab main-slab-detail">
      1 pc
    </span>
    Margin ₹ 24.00 | 16.00%
  </div>
</div>
TEST;

$html=new simple_html_dom();

$html->load($cl);

$ret = $html->find("div.mrp.ml-md-4.ml-lg-2.pd-mrp",0)->plaintext;

print_r($ret);

?>

it outputs :

₹ 150.00
  • Related