简体   繁体   中英

How to scrape using html class name in php crawler?

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]');

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM