2017-02-16 60 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元素。我將在上面編輯我的帖子,以獲取完整的工作示例。 –