【问题标题】:Using getlementbyclass name or getlementbytag to scrape data from html content使用 getlementbyclass 名称或 getlementbytag 从 html 内容中抓取数据
【发布时间】:2014-04-28 04:10:31
【问题描述】:

这里我从网页中获取了源代码截图:http://www.yelp.com/biz/franchino-san-francisco?start=80

我想为页面上的每个块抓取日期、评论、评分。

@:http://ideone.com/fork/Yfw2re

我对 DOM 元素不太熟悉,如果有人可以纠正这个问题,我很感激

代码如下:

<?php

// your code goes here    
$html = <<< EOF    
<div class="review-wrapper">
           <div class="review-content">
        <div class="biz-rating biz-rating-very-large clearfix">
    <div itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating">

    <div class="rating-very-large">
    <i class="star-img stars_5" title="5.0 star rating">
        <img alt="5.0 star rating" class="offscreen" height="303" src="http://s3-media3.ak.yelpcdn.com/assets/2/www/img/c2252a4cd43e/ico/stars/v2/stars_map.png" width="84">
    </i>
        <meta itemprop="ratingValue" content="5.0">
</div>   
    </div>
        <span class="rating-qualifier">
        <meta itemprop="datePublished" content="2013-10-28">
    10/28/2013
</span>    
</div>   
            <p class="review_comment ieSucks" itemprop="description" lang="en">The reason I started a yelp account, was to write a review for Franchinos. This is my favorite restaurant in the city of San Francisco, and especially, North Beach. <br><br>Where do I start... I take every friend, family member and acquaintance to Franchinos in every opportunity I can. I am a Italy-nut and have been over three times - the mood + atmosphere is almost identical. It is a 100% family-run restaurant and you can taste the expertise and &#39;home-cooking&#39;. <br><br>Each time, I get a large bottle of wine (One time - they ran out of the wine I had ordered - and instead gave me a larger, more expensive bottle - same price), a wonderful pasta dish (Alfredo, carbonara.. etc.) and a Caesar salad.<br><br>Need I say more? Buenisimo. I look forward to the next time.. and the times after that again and again. <br><br>è perfetto!</p>     
</div>
<div class="review-footer clearfix">
               <div class="rateReview ufc-feedback clearfix" data-review-id="SnZ4Q97nJdR7a-fot-Slcw">
                <p class="review-intro review-message">
    Was this review &hellip;?
</p>
EOF;


$dom = new DOMDocument();
@$dom->loadHTML($html);    
$classname = 'review-content'
$finder = new DomXPath($dom);
$nodes = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");
$tmp_dom = new DOMDocument();      
foreach($nodes as $result) {
 
  //getting rate value from '<meta itemprop="ratingValue" content="5.0">'
  //getting  date from <span class="rating-qualifier">       <meta itemprop="datePublished" content="2013-10-28">     10/28/2013 </span>
  //getting review from  ' <p class="review_comment ieSucks" itemprop="description" lang="en">The reason I started a yelp account, was to write a review for Franchinos. This is my favorite restaurant in the city of San Francisco, and especially, North Beach. <br><br>Where do I start... I take every friend, family member and acquaintance to Franchinos in every opportunity I can. I am a Italy-nut and have been over three times - the mood + atmosphere is almost identical. It is a 100% family-run restaurant and you can taste the expertise and &#39;home-cooking&#39;. <br><br>Each time, I get a large bottle of wine (One time - they ran out of the wine I had ordered - and instead gave me a larger, more expensive bottle - same price), a wonderful pasta dish (Alfredo, carbonara.. etc.) and a Caesar salad.<br><br>Need I say more? Buenisimo. I look forward to the next time.. and the times after that again and again. <br><br>è perfetto!</p> '
 
}

【问题讨论】:

    标签: php regex dom


    【解决方案1】:

    您可以像这样遍历class 值或tag 名称:

    $classname = 'rating-qualifier';
    $dom = new DOMDocument;
    $dom->loadHTML($html);
    $xpath = new DOMXPath($dom);
    $results = $xpath->query("//*[@class='" . $classname . "']");
    
    if ($results->length > 0) {
        echo $review = $results->item(0)->nodeValue;
    }
    
    
    $classname = 'review_comment ieSucks';
    $dom = new DOMDocument;
    $dom->loadHTML($html);
    $xpath = new DOMXPath($dom);
    $results = $xpath->query("//*[@class='" . $classname . "']");
    
    if ($results->length > 0) {
        echo $review = $results->item(0)->nodeValue;
    }
    
    $meta = $dom->documentElement->getElementsByTagName("meta");
    echo $meta->item(0)->getAttribute('content');
    

    您显然可以使用简单的for 循环循环评分部分以获取页面上的所有评分。

    在这里演示:https://eval.in/143036

    【讨论】:

    • 或者可以预先卷曲页面并使用您的命令读取内容
    • 好吧,它认为 html 中有一些字符使字符串在两者之间终止,您必须找到这样的 ' 并使用反斜杠将它们转义,以便整个 html 源代码表现正常字符串
    • @user123 是的哥们我删除了 /div 和 /li 然后解析了代码
    • 感谢您的帮助。您启动了解决方案。我达到了我想要的目标
    • @user123 由于php5循环引用内存泄漏,创建DOM对象后,如果多次调用file_get_dom(),必须调用$dom->clear()释放内存。
    猜你喜欢
    • 2021-12-22
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    相关资源
    最近更新 更多