我需要使用xpath查询解析以下示例html。
<td id="msgcontents">
<div class="user-data">Just seeing if I can post a link... please ignore post
<a href="http://finance.yahoo.com">http://finance.yahoo.com</a>
</div>
</td>
<td id="msgcontents">
<div class="user-data">some text2...
<a href="http://abc.com">http://abc.com</a>
</div>
</td>
<td id="msgcontents">
<div class="user-data">some text3...
</div>
</td>上面的html可以在页面中重复no。
有时.如上述html块所示,部分可能不存在。
我需要的是xpath语法,这样我就可以将解析的字符串作为
array1[0]= "Just seeing if I can post a link... please ignore post ttp://finance.yahoo.com"
array[1]="some text2 htp://abc.com"
array[2]="sometext3" 发布于 2010-09-28 07:23:58
也许类似于以下几点:
$remote = file_get_contents('http://www.sitename.com');
$dom = new DOMDocument();
//Error suppression unfortunately, as an invalid xhtml document throws up warnings.
$file = @$dom->loadHTML($remote);
$xpath = new DOMXpath($dom);
//Get all data with the user-data class.
$userdata = $xpath->query('//*[contains(@class, \'user-data\')]');
//get links
$links = $xpath->query('//a/@href');因此,要访问其中一个变量,需要使用nodeValue
$ret = array();
foreach($userdata as $data) {
$ret[] = $data->nodeValue;
}编辑:,我想我应该提一下,这会在给定的页面上得到所有的链接,我想这就是你想要的吗?
发布于 2010-09-28 12:41:49
使用
concat(/td/div/text[1], ' ', /td/div/a)您可以使用代替上面的‘’,这是您希望出现在两个字符串之间的任何分隔符。
https://stackoverflow.com/questions/3810471
复制相似问题