我编写了一个代码,搜索特定的文档名(例如: SZ-1000)并收集div class="box"包含的所有链接。(index.php)文档名可能包含一个或两个带有ID的文档。(26904,26905)它的工作方式类似于一个魅力。我把身份证拿回来。
但是我想列出链接作为链接包含的所有附件。诀窍是,现在没有div元素,只有table或dd来指定附件位置。
我觉得那部分有问题:
$xpath->query('//table[@id="content clear-block"]');
Catchable fatal error: Object of class DOMNodeList could not be converted to string in C:\AppServ\www\test\import.php on line 35var_dump($articles)在import.php中的应用结果;
object(DOMNodeList)#3 (0) { }
object(DOMNodeList)#3 (0) { }我的代码:
index.php
$site = 'http://192.168.0.1:81/?q=search/node/SZ-1000';
$html = file_get_contents($site);
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXpath($doc);
$articles = $xpath->query('//div[@class="box"]');
$links = array();
foreach($articles as $container) {
$arr = $container->getElementsByTagName("a");
foreach($arr as $item) {
$href = $item->getAttribute("href");
$links[] = $href;
}
}
foreach($links as $link){
$text = end(split('/',$value));
echo $text."<br>";
$wr_out = file_get_contents("http://127.0.0.1/test/import.php?value=".$text);
echo $wr_out;
}import.php
$value = $_GET['value'];
$site = 'http://192.168.0.1:81/?q=node/'.$value;
$html = file_get_contents($site);
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXpath($doc);
$articles = $xpath->query('//table[@id="content clear-block"]');
$links = array();
foreach($articles as $container) {
$arr = $container->getElementsByTagName("a");
foreach($arr as $item) {
$href = $item->getAttribute("href");
$links[] = $href;
echo $href;
}
}谢谢你的回复!
编辑:
“可捕获的致命错误: DOMNodeList类的对象无法在第35行的C:\AppServ\www\test\import.php中转换为字符串”
错误修正:
echo $wr_out->tagName;
发布于 2015-04-08 14:43:14
哦,我终于成功了。这是后世的解决办法。用UTF-8字符编码。
index.php
<?php//从外部源获取一些变量我在Google电子表格中使用它
$get = $_GET['get'];
$site = 'http://192.168.0.1:81/?q=search/node/'.$get;
$html = file_get_contents($site);
//libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXpath($doc);
$articles = $xpath->query('//div[@class="box"]');
if(!empty($articles)){
$links = array();
foreach($articles as $container) {
$arr = $container->getElementsByTagName("a");
foreach($arr as $item) {
$href = $item->getAttribute("href");
$links[] = $href;
}
}
$wr_out = "";
foreach($links as $value){
$text = end(split('/',$value));
$wr_out.=file_get_contents("http://127.0.0.1/projekt/search/import.php?value=".$text);
}
if(empty($wr_out))
echo "There is no document with that ID";
else
echo $wr_out;
}
else
echo "There is no document with that ID";
?>import.php
$value = $_GET['value'];
$site = 'http://192.168.0.1:81/?q=node/'.$value;
$html = file_get_contents($site);
//libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTML($html);
$elements = $doc->getElementsByTagName('tbody');
$table = $elements->item(0);
$rows = $table->childNodes;
foreach ($rows as $node) {
if($node->tagName == "tr"){
$a = $node->firstChild->firstChild;
foreach ($a->attributes as $attr) {
if($attr->nodeName == "href"){
$value = $attr->nodeValue;
?>
<!doctype html>
<head>
<title>Search</title>
<meta charset="UTF-8">
</head>
<body>
<table align="center">
<tr>
<td></td>
<td class="style-1">
<br><h3>
<?=$value?> | <a href='<?=$value?>'>LINK</a></h3><hr>
</td>
</tr>
</table>
</body><?
}
}
}
}?>https://stackoverflow.com/questions/29506822
复制相似问题