使用简单的html dom找到facebook用户名 - php

时间:2014-06-24 14:05:47

标签: php web-scraping simple-html-dom

我使用的是php和简单的html dom

http://simplehtmldom.sourceforge.net/manual.htm

我试图从下面的addthis代码中提取facebook,twitter,google,youtube和pinterest用户名。

<!-- AddThis Follow BEGIN -->
<div class="addthis_toolbox addthis_default_style" style="margin-top:-3px;">  
<a tabindex="2" class="addthis_button_facebook_follow" style="padding:3px 6px;" addthis:userid="namehere"></a>
<a tabindex="3" class="addthis_button_twitter_follow" style="padding:3px 6px;"  addthis:userid="namehere"></a>
<a tabindex="4" class="addthis_button_google_follow" style="padding:3px 6px;"  addthis:userid="namehere"></a>
<a tabindex="5" class="addthis_button_youtube_follow" style="padding:3px 6px;"  addthis:userid="namehere"></a>
<a tabindex="6" class="addthis_button_pinterest_follow" style="padding:3px 6px;"  addthis:userid="namehere"></a>
</div>
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-51e6691575bd9b10"></script>
<!-- AddThis Follow END -->

如何使用简单的html dom获取用户名?

我试过这个......

$html = file_get_html('http://www.domain.com/');
foreach($html->find('a[class=addthis_button_facebook_follow]') as $element)
echo $element->class;

输出类名:

addthis_button_facebook_follow

但是如何返回addthis:userid值?

addthis:userid

2 个答案:

答案 0 :(得分:2)

实际上,您并未正确调用该属性。考虑这个例子:

// target the first link, since you only need one, and target the proper attribute
$id = $html->find('a[class="addthis_button_facebook_follow"]', 0)->{'addthis:userid'}; // first link facebook
echo $id; // namehere

答案 1 :(得分:1)

尝试:

echo $element->{'addthis:userid'};