【问题标题】:using simple html dom to find facebook username - php使用简单的 html dom 查找 facebook 用户名 - php
【发布时间】:2014-08-14 20:10:58
【问题描述】:

我正在使用 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

【问题讨论】:

  • 试试这个:$name = 'addthis:userid'; echo $element-&gt;$name;

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


【解决方案1】:

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

// 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

【讨论】:

    【解决方案2】:

    试试:

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

    【讨论】:

      猜你喜欢
      • 2018-02-03
      • 1970-01-01
      • 2015-12-08
      • 1970-01-01
      • 2019-12-18
      • 2018-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多