DOM XPath查询返回没有
问题描述:
我试着检索电子邮件地址,有些ID的人没有一个电子邮件地址,因此不具备$ gp_rowDOM XPath查询返回没有
当我的var_dump的$ gprow我只接收字符串( x)的电子邮件地址,目的是做一个CSV文件,有没有办法只返回一个逗号,如果它不能从路径返回字符串?
在此先感谢
<?php
$count = 0;
while($count <= 3) {
$emails = array(508561, 521174, 470245, 535282);
$html = file_get_contents('http://xxxxxxxxxx.xxxx/office/'. $emails[$count] .'');
$gp_doc = new DOMDocument();
libxml_use_internal_errors(TRUE); //disable libxml errors
$gp_xpath = new DOMXPath($gp_doc);
$gp_row = $gp_xpath->query('//dd[6]/a');
foreach($gp_row as $row){
$file = '1.csv';
$current = file_get_contents($file);
$current .= $row->nodeValue . ", ";
$trimmed = ltrim($current, ",");
file_put_contents($file, $trimmed);
}
$count++;
}
?>
答
补充一点:$gp_doc->loadHTML($html);
你被quering空文件,你必须添加此$html
。如果每一个都是正确的,你的代码将会正常工作。
<?php
$count = 0;
while($count <= 3) {
$emails = array(508561, 521174, 470245, 535282);
$html = file_get_contents('http://xxxxxxxxxx.xxxx/office/'. $emails[$count] .'');
$gp_doc = new DOMDocument();
$gp_doc->loadHTML($html); // add here
libxml_use_internal_errors(TRUE); //disable libxml errors
$gp_xpath = new DOMXPath($gp_doc);
$gp_row = $gp_xpath->query('//dd[6]/a');
foreach($gp_row as $row){
$file = '1.csv';
$current = file_get_contents($file);
$current .= $row->nodeValue . ", ";
$trimmed = ltrim($current, ",");
file_put_contents($file, $trimmed);
}
$count++;
}
?>
答
我认为,唯一的机会就是抓住存在于所有的电子邮件(IDS)的元素,所以也许dd
,甚至它的父,然后从那里如果a
存在检查。如果它不添加空结果,否则电子邮件。
没有改变任何东西 – Bill
@Bill你能分享你的'print_r($ html)'吗? –