如何使用PHP Simple HTML DOM从$ _GET项跟踪的远程页面获取数据?

时间:2013-08-10 01:13:07

标签: php dom simple-html-dom

点击A.html中的链接时:

<tr>
<td class="book"><a class="booklink" ref="../collection?file=book1.pdf">
Good Read
</a>
-Blah blah blah
</td>  
</tr>

?file = book1.pdf传递给B.html:

<?php
$src = $_GET['file'];
?>

<iframe src="<?php echo $src; ?>" >
</iframe>

问题: - 如何从A.html中检索文本“Good Read-Blah blah blah blah”并使用简单的html dom将其粘贴到B.html中的元描述中? (请注意,A.html表格中有数千种列出的数据)

谢谢。

1 个答案:

答案 0 :(得分:0)

使用DOMload your HTML documentXPath to search it

// note: if the HTML to parse has bad syntax, use: libxml_use_internal_errors(true);

$doc = new DOMDocument;
$doc->loadHTML(file_get_contents('A.html'));
if ($doc === false) {
    throw new RuntimeException('Could not load HTML');
}

$xpath = new DOMXPath($doc);
$xpathResult = $xpath->query("//a[@href = '../collection?file={$_GET['file']}']/..");
if ($xpathResult === false) {
    throw new LogicException('Something went wrong querying the document!');
}

foreach ($xpathResult as $domNode) {
    echo 'Link text: ' . htmlentities($domNode->textContent) . PHP_EOL;
}