2017-02-16 49 views
-1
<span class="contact-seller-name">Enda</span> 

现在我想用PHP我想分析跨度的innerText在PHP中使用simple_html_dom

呼应 '恩达' 这个span标记内这里是我的PHP代码

$url="http://website.example.com"; 

$html = file_get_html($url); 

$value = $html->find('span.contact-seller-name'); 

echo $value->innertext; 
+0

可以请你详细说明你的意思是通过回声成什么:

全部工作例如在直播现场测试? –

+1

您的代码中没有定义变量'$ post' – empiric

+0

现在我已更正了我的代码。我想在范围标记中回显'Enda' – Ahmed

回答

0

从他们的文档它看起来像找到返回数组匹配过滤器参数的匹配值:

来自:

http://simplehtmldom.sourceforge.net/

代码:

// Find all images 
foreach($html->find('img') as $element) 
     echo $element->src . '<br>'; 

他们还为获得一个特定的元素提供了另一个例子:

$html->find('div[id=hello]', 0)->innertext = 'foo'; 

所以我的猜测是这样的事情会得到你想要你的愿望:

$value = $html->find('span.contact-seller-name', 0); 

echo $value->innertext; 

通过广告将其作为参数返回该过滤器的第一个找到的实例。

在它们的API看一看这里:

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

它描述什么找到方法返回(元素对象或元对象组成的数组,如果第二参数定义)

然后,使用为元素对象提供的任何方法都可以获得所需的文本。

$url = "http://fleeceandthankyou.org/"; 

$html = file_get_html($url); 

$value = $html->find('span.givecamp-header-wide', 0); 

//If it can't find the element, throw an error 
try 
{ 
    echo $value->innertext; 
} 
catch (Exception $e) 
{ 
    echo "Couldn't access magic method: " . $e->getMessage(); 
} 
+0

它给我一个错误:“尝试在第11行中获取D:\ Programs \ xampp \ htdocs \ personal \ WebScrapping \ test3.php中的非对象属性”代码是:echo $ value-> innertext; – Ahmed

+0

它看起来像您正在寻找的DOM元素没有被解析器找到。请检查您的查询标记,或确保页面加载时有DOM元素。我将在上面编辑我的帖子,以获取完整的工作示例。 –