使用 php 从 url 检索 href 属性

retrieve href attributes from url with php

我想获取website1提供的首页url所有锚标签内的href属性,按一级深度抓取网站,获取所有锚标签内的href属性在已抓取的页面上找到锚标记,但未显示任何内容。我使用的函数是 findAndCompare.

<html>
<body>

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
website: <input type="text" name="website1"><br>
website: <input type="text" name="website2"><br>
<input type="submit" name="submit">
</form>

</body>
</html> 

<?php
  if(isset($_POST['submit']))
  {
    // form has been submitted
    $form_data = $_POST['website1'];
    findAndCompare($form_data);

  }
  else
  {}

function findAndCompare($url){

// Create a DOM parser object
$dom = new DOMDocument();

$dom->loadHTML($url);

// Iterate over all the <a> tags
foreach($dom->getElementsByTagName('a') as $link) {
        // Show the <a href>
        echo $link->getAttribute('href');
        echo "<br />";
}
}

?>

方法loadHTML() requires HTML source, not an URL. You could load the source with loadHTMLFile()改为:

function findAndCompare($url){

    // Create a DOM parser object
    $dom = new DOMDocument();

    // load HTML from URL
    $dom->loadHTMLFile($url);

    // Iterate over all the <a> tags
    foreach($dom->getElementsByTagName('a') as $link) {
        // Show the <a href>
        echo $link->getAttribute('href');
        echo "<br />";
    }
}