使用PHP Simple HTML DOM获取img src

时间:2014-07-02 08:49:19

标签: php html dom jquery-lazyload

Demo

我需要从以下代码中获取图像src

HTML

<div class="avatar profile_CF48B2B4A31B43EC96F0561F498CE6BF ">
    <a onclick="">
        <img id="lazyload_-247847544_0" height="74" width="74" class="avatar potentialFacebookAvatar avatarGUID:CF48B2B4A31B43EC96F0561F498CE6BF" src="http://media-cdn.tripadvisor.com/media/photo-l/05/f3/67/c3/lilrazzy.jpg" />
    </a>
</div>

我尝试写 js

foreach($html->find('div[class=profile_CF48B2B4A31B43EC96F0561F498CE6BF] a img') as $element) {
    $img = $element->getAttribute('src');
    echo $img;
}

但它显示src键不存在。如何废弃审查头像图片?

更新

查看页面来源时找不到图片网址,但 firebug 会显示图片网址:

<img id='lazyload_1953171323_17' height='24' alt='4 helpful votes' width='25' class='icon lazy'/>

以下是我的网页来源代码:

<div class="col1of2">
<div class="member_info">
<div id="UID_3E0FAF58557D3375508A9E5D9A7BD42F-SRC_175428572" class="memberOverlayLink" onmouseover="ta.trackEventOnPage('Reviews','show_reviewer_info_window','user_name_photo'); ta.call('ta.overlays.Factory.memberOverlayWOffset', event, this, 's3 dg rgba_gry update2012', 0, (new Element(this)).getElement('.avatar')&&(new Element(this)).getElement('.avatar').getStyle('border-radius')=='100%'?-10:0);">
<div class="avatar profile_3E0FAF58557D3375508A9E5D9A7BD42F ">
<a onclick=>
<img id='lazyload_1953171323_15' height='74' width='74' class='avatar potentialFacebookAvatar avatarGUID:3E0FAF58557D3375508A9E5D9A7BD42F'/>
</a>
</div>
<div class="username mo">
<span class="expand_inline scrname hvrIE6 mbrName_3E0FAF58557D3375508A9E5D9A7BD42F" onclick="ta.trackEventOnPage('Reviews', 'show_reviewer_info_window', 'user_name_name_click')">Prataspeles</span>
</div>
</div>
<div class="location">
Latvia
</div>
</div>
<div class="memberBadging">
<div id="UID_3E0FAF58557D3375508A9E5D9A7BD42F-CONT" class="totalReviewBadge badge no_cpu" onclick="ta.trackEventOnPage('Reviews','show_reviewer_info_window','review_count'); ta.util.cookie.setPIDCookie('15984'); ta.call('ta.overlays.Factory.memberOverlayWOffset', event, this, 's3 dg rgba_gry update2012', -10, -50);">
<div class="reviewerTitle">Reviewer</div>
<img id='lazyload_1953171323_16' height='24' alt='4 reviews' width='25' class='icon lazy'/>
<span class="badgeText">4 reviews</span>
</div>
<div id="UID_3E0FAF58557D3375508A9E5D9A7BD42F-HV" class="helpfulVotesBadge badge no_cpu" onclick="ta.trackEventOnPage('Reviews','show_reviewer_info_window','helpful_count'); ta.util.cookie.setPIDCookie('15983'); ta.call('ta.overlays.Factory.memberOverlayWOffset', event, this, 's3 dg rgba_gry update2012', -22, -50);">
<img id='lazyload_1953171323_17' height='24' alt='4 helpful votes' width='25' class='icon lazy'/>
<span class="badgeText">4 helpful votes</span>
</div>
</div>
</div> 

因使用lazyload会有什么问题吗?

更新2

使用lazyload会在页面加载后加载我的图片,我尝试获取图片ID并将其与 lazyload js 数组进行比较,但此ID与不匹配lazyload var 数组。

问题:

如何从 JSON 获取此js数组?

示例:

{"id":"lazyload_-205858383_0","tagType":"img","scroll":true,"priority":100,"data":"http://media-cdn.tripadvisor.com/media/photo-l/05/f3/67/c3/lilrazzy.jpg"}
,   {"id":"lazyload_-205858383_1","tagType":"img","scroll":true,"priority":100,"data":"http://c1.tacdn.com/img2/icons/gray_flag.png"}
,   {"id":"lazyload_-205858383_2","tagType":"img","scroll":true,"priority":100,"data":"http://media-cdn.tripadvisor.com/media/photo-l/01/2a/fd/98/avatar.jpg"}
,   {"id":"lazyload_-205858383_3","tagType":"img","scroll":true,"priority":100,"data":"http://c1.tacdn.com/img2/icons/gray_flag.png"}
,   {"id":"lazyload_-205858383_4","tagType":"img","scroll":true,"priority":100,"data":"http://media-cdn.tripadvisor.com/media/photo-l/01/2e/70/5e/avatar036.jpg"}
,   {"id":"lazyload_-205858383_5","tagType":"img","scroll":false,"priority":100,"data":"http://c1.tacdn.com/img2/badges/badge_helpful.png"}

5 个答案:

答案 0 :(得分:4)

您遇到困难,因为一旦页面加载,javascipt用于延迟加载图像。使用phpDom查找元素的Id,然后使用正则表达式根据此Id查找相关图像。

要实现这一目标,请尝试以下方法:

$json = json_decode("<JSONSTRING HERE>");

foreach($html->find('div[class=profile_CF48B2B4A31B43EC96F0561F498CE6BF] a img') as $element) {
   $imgId = $element->getAttribute('id');

   foreach ($json as $lazy)
   {
      if ($lazy["id"] == $imgId) echo $lazy["data"];
   }
}

以上是未经测试的,因此您需要解决这些问题。它们的关键是提取相关的javascript并将其转换为json。

或者,您可以使用字符串搜索功能来获取包含有关img信息的行,并提取所需的值。

答案 1 :(得分:3)

如果您要查找包含子字符串“lazyload”的所有ID,您可以尝试使用通配符选择器,然后查看找到的元素的'src'属性。请参阅下面的jsfiddle。祝你好运!

$(document.body).find('img[id*=lazyload]').each(function() {
   console.log($(this).prop('src'));
});

Jsfiddle

答案 2 :(得分:1)

试试这个 -

foreach($html->find('div[class=profile_CF48B2B4A31B43EC96F0561F498CE6BF ] a img') as $element) {
$img = $element->getAttribute('src');
echo $img;
}

班级名称后面有空格。您必须在类名末尾添加空格。

OR

甚至使用全班名称

$html->find('div[class=avatar profile_CF48B2B4A31B43EC96F0561F498CE6BF ] a img'

答案 3 :(得分:1)

使用jQuery选择器,即$(&#39;#lazyload_-247847544_0&#39;),您可以使用此获取图像源

var src = $('#lazyload_-247847544_0').attr('src');

或更具体地说

$('.profile_CF48B2B4A31B43EC96F0561F498CE6BF #lazyload_-247847544_0').attr('src');

由于

答案 4 :(得分:0)

function getReviews(){

    $url = 'http://www.tripadvisor.com/Hotel_Review-g274965-d952833-Reviews-Ezera_Maja-Liepaja_Kurzeme_Region.html';
    $html = new simple_html_dom();
    $html = file_get_html($url);
    $array = array();
    $i = 0;

   // IMG ID
    foreach($html->find('div[class=avatar] a img') as $element) {  $array[$i]['id']  = $element->getAttribute('id'); $i++;} unset($i);$i = 0;

    // IMG SRC
    $p1 = strpos( $html, 'var lazyImgs =' ) + 14;
    $p2 = strpos( $html, ']', $p1  );
    $raw = substr( $html, $p1, $p2 - $p1 ) . ']';
    $images = json_decode($raw);

    foreach ($images as $image){

        $id     = $image->id;
        $data   = $image->data;
        foreach ($array as $element){
            if ( isset($element['id']) && $element['id'] == $id){
                $array[$i]['image'] = $data;
                $i++;    
            }
        }
    }

    $html->clear();
    unset($html);
    return $array;
}

获取数组中的IMG ID。然后在json中解压var Lazyload并解码。然后比较2个数组,如果id mach将数据添加到数组。 谢谢大家!