【发布时间】:2020-06-01 18:45:54
【问题描述】:
如何使用 php 从远程 HTML 页面中检索特定元素和属性?
例如,如果要检索的元素和属性具有以下格式:
<a href="/dir/someid/" class="ccc">
任何帮助将不胜感激。
将使用的代码方法:
<?php
$file = fopen ("http://www.example.com/", "r");
if (!$file) {
echo "<p>Unable to open remote file.\n";
exit;
}
while (!feof ($file)) {
$line = fgets ($file, 1024);
/* This only works if the title and its tags are on one line */
if (preg_match ("@\<title\>(.*)\</title\>@i", $line, $out)) {
$title = $out[1];
break;
}
}
fclose($file);
?>
【问题讨论】:
-
查看XPath。
-
解决方法是使用DOMXpath。
标签: php html web-crawler extract