php简单的html dom获取td内的href id

问题描述:

如何使用php简单的html dom获取href里面的“name”或“id”属性。另外我需要“标题文本”在“h4”标签内。 你能帮我吗? 下面是HTML:php简单的html dom获取td内的href id

<td> 
<a href="../Vehicle?itemID=22995061&RowNumber=9&loadRecent=True" name="22995061" id="22995061"> 
<h4>title text</h4> 
</a> 
<p> 
Stock#: 
<text>example</text> 
</p> 
<p>BLA BLA</p> 
<p> fffff </p> 
</td> 

我想这样的事情,但它返回我的空白。

IDs = array(); 
    $url = "http://someurl"; 
    $html = file_get_html(url); 
foreach($html->find('h4') as $e) 
{ 

    echo $e->innertext."<br>"; 
    $dataID = $e->innertext; 
    $IDs[] = $dataID; 

} 

首先,变化,

IDs = array(); 

到,

$IDs = array(); 

那么,你为什么不利用DOMDocument类的,而不是一个正则表达式。只需加载您的DOM,然后使用getElementsByTagName来获取您的标签。通过这种方式,您可以排除任何您不需要的其他标签,只会获得您所需的标签。

<?php 
$xml = <<< XML 
<?xml version="1.0" encoding="utf-8"?> 
<books> 
<book>Patterns of Enterprise Application Architecture</book> 
<book>Design Patterns: Elements of Reusable Software Design</book> 
<book>Clean Code</book> 
</books> 
XML; 

$dom = new DOMDocument; 
$dom->loadXML($xml); 
$books = $dom->getElementsByTagName('book'); 
foreach ($books as $book) { 
    echo $book->nodeValue, PHP_EOL; 
} 
?> 

阅读材料

DOMDocument

+0

嗨,TNX的答复,但我使用PHP简单的HTML DOM抢数据,因为我是我代理后面。当我在代理服务器后面时,我不知道如何使用php DOM来获取URL。 – dilesko