-2

I want the rating of this book which is mentioned in the name of class. So, how can I get that class name?

<p class ="star_rating five"> </p>

$prices = $xpath->evaluate('//ol[@class="row"]//li//article//div[@class="product_price"]//p[@class="price_color"]');

$rating =$xpath->evaluate('//ol[@class="row"]//li//article//p[@class]');
OMi Shah
  • 5,768
  • 3
  • 25
  • 34
  • Anything not working with the given code? – Nico Haase Nov 18 '22 at 14:57
  • I suggest you add more details to your question. What does the variable `$xpath` contain? Is there a snippet of HTML that you are parsing? – ryantxr Nov 18 '22 at 15:07
  • It looks pretty obvious that the given xpath does not work: there's no `ol` tag in the code you've shared, there's no `li` tag, no `article` tag, no `div` – Nico Haase Nov 18 '22 at 15:20

1 Answers1

0

You can use the following example and it would work fine.

$html = '<div><p class ="star_rating five"> </p><div>';
if (!empty($html)) {
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    $xpath = new DOMXPath($dom);
 
    $desiredTag = "p";
 
    foreach ($xpath->query('//'.$desiredTag) as $node) {
        $p = $node->getAttribute('class');
        echo $p;
    }
}

You can filter out the tags you want specifically & reach to the

node which contains your desired attribute, using getAttribute you can get the value provided in it.

try above provided snippet: https://ideone.com/aG8DcN

Waqas Mustafa
  • 192
  • 1
  • 7
  • actually its not working ... i dont need attribute of classname i just want name of class name as a result.. – Vupesh Awasthi Nov 18 '22 at 15:42
  • The name of class 'star_rating five' right ? – Waqas Mustafa Nov 18 '22 at 16:02
  • @VupeshAwasthi Have you tried the snippet link & checked the output ? – Waqas Mustafa Nov 18 '22 at 16:05
  • just one more query how this code work on class inside another class.. for example '

    ';....please help me
    – Vupesh Awasthi Nov 18 '22 at 16:17
  • check this it works fine with your specific case as well: https://ideone.com/wfR6n9 – Waqas Mustafa Nov 18 '22 at 16:37
  • You better post issue / error / exception you're getting or provide your whole case I suspect you're making some other mistakes because the above snippet will work in any case it's querying the whole DOM and not just simple one div. – Waqas Mustafa Nov 18 '22 at 16:39
  • this is working fine...i need a solution which have two class..class inside another class.. – Vupesh Awasthi Nov 18 '22 at 16:39
  • give us an example ? You need to understand the code. Once you have loaded the DOM into an object you can query anything even if it's nested and depth level is too deep. – Waqas Mustafa Nov 18 '22 at 16:41
  • its not working in class inside class.(nested) ...for example go to this https://books.toscrape.com/catalogue/category/books/science_22/index.html... crawl the rating of each book... – Vupesh Awasthi Nov 18 '22 at 16:43
  • At this point can't help you unless you share your code or more details to the original question. – Waqas Mustafa Nov 18 '22 at 16:44
  • its not working in class inside class.(nested) ...for example go to this https://books.toscrape.com/catalogue/category/books/science_22/index.html... crawl the rating of each book... – Vupesh Awasthi Nov 18 '22 at 16:45
  • yes you need to get the
      first then loop the
    1. and then go to the article and so on, that's how you will get the start rating classes.
    – Waqas Mustafa Nov 18 '22 at 17:14