【问题标题】:how to get all href links from web url如何从网址获取所有href链接
【发布时间】:2018-03-22 08:23:35
【问题描述】:

我想从包含文件的 web url(url:http://localhost/test.php) 获取所有 href 链接,因为我已经包含了一个 php 文件 (menulink.php) 我的脚本仅适用于 href 之外的情况,如果条件检查了 href。

<a href="test.html" title="test"></a>
<a href="test1.html" title="test1"></a>
<a href="menulink1.html" title="menulink1"></a>
<a href="menulink2.html" title="menulink2"></a>

如果条件为真:

 <a href="test4.html" title="test4"></a>
 <a href="menulink3.html" title="menulink3"></a>

但我也想同时获得 else 条件(我需要所有 href 链接不考虑 if else 条件或任何条件)

缺少 href 链接:

<a href="http://www.google.com" title="test6"></a>
<a href="menulink5.html" title="menulink5"></a>
<a href="menulink6.html" title="menulink6"></a>

我该怎么做?你能从我的代码中解决吗? 这是我的代码:

index.html

         $(document).ready(function(){

        var url = "http://localhost/links/test.php"; 
        var arr= null;
         $.post('load.php', { url: url,async: false},
            function(html) {
                $('#page').html(html);  
                var links = $('a');
                var title = $('a').attr('title');  

                var arr=[];
                for(var i=0; i < links.length; i++)
                {
                    arr.push(links[i]); 
                }
                console.log(arr); //all href links//  
        }  );

    });

test.php

  <html>
  <head>
  <title>Test</title>
  </head>
  <body>
  <div>
  <?php include("menulink.php"); ?>

   <a href="test.html" title="test"></a>
   <a href="test1.html" title="test1"></a>
   <?php
   $result_array="123";
   if ($result_array=="123")
   {
   ?>
   <a href="test4.html" title="test4"></a>
   <?php 
   }
   else{
   ?>
   <a href="http://www.google.com" title="test6"></a>
   <?php
   } 
   ?>
   </div>
   </body>
   </html>

加载.php

<?php
$url = $_POST['url'];
$html = file_get_contents($url);
echo $html;
?>

menulink.php

  <html>
  <head>
  <title>Test</title>
  </head>
  <body>
  <div>
  <a href="menulink1.html" title="menulink1"></a>
  <a href="menulink2.html" title="menulink2"></a> 
  <?php
  $result_array="123";
  if ($result_array=="123")
  {
  ?>
  <a href="menulink3.html" title="menulink3"></a>

  <?php 
  }
  else{
  ?>
  <a href="menulink5.html" title="menulink5"></a>
  <a href="menulink6.html" title="menulink6"></a>
  <?php
  }
  ?>

 </div>
 </body>
 </html>

我想获取所有的 href 链接。不要考虑是否存在其他条件或 url 文档中的任何条件。请从我的代码中解析。

【问题讨论】:

    标签: javascript php jquery angularjs html


    【解决方案1】:

    试试这个:

         $dom = new DOMDocument;
         $dom->validateOnParse = true;
         $dom->loadHTML($html);
         $links = array(); 
         foreach($dom->getElementsByTagName('a'); as $link) { 
            $links[] = $link->getAttribute('href'); 
         } 
    

    【讨论】:

    • 在你的 load.php 中抱歉我忘记添加了
    • 你能不能 var_dump($links); ?
    • 不工作..请编辑 load.php 并发送完整代码
    【解决方案2】:
    // Create DOM from URL or file //you have to include simple_html_dom.php form above link
    
    $html = file_get_html('http://www.google.com/');
    
    
    
    // Find all links 
    

    你必须在 load.php 文件中这样做

             include("simple_html_dom.php");  // please make sure right location of file 
    $url = $_POST['url'];
    $html = file_get_html($url);
    header('Content-Type: application/json');
    $links = array(); 
    foreach($html->find('a') as $element) {
        $links[] = $element->href;
        } echo json_encode($links);
    

    在 jquery 中获取这些链接

     $(document).ready(function(){
    
            var url = "http://localhost/links/test.php"; 
            //var arr= null;
             $.post('load.php', { url: url,async: false},
                function(html) {
                    $('#page').html(html);  
                    console.log(html);
            }  );
    
        });
    
    HTML
    
            <html>
          <head>
          <title>Test</title>
          </head>
          <body>
          <div>
    
    
          <?php include("menulink.php"); ?>
    
           <a href="test.html" title="test"></a>
           <a href="test1.html" title="test1"></a>
           <?php
           get_links("123");
           get_links("321");
            function get_links($a){
                ?>
                <?php
           $result_array=$a;
           if ($result_array=="123")
           {
           ?>
           <a href="test4.html" title="test4"></a>
           <?php 
           }
           else{
           ?>
           <a href="http://www.google.com" title="test6"></a>
           <?php
           } 
           ?>
                <?php
            }
    }
           ?>
           </div>
           </body>
           </html>
    

    【讨论】:

    • 请阅读我的问题。你的脚本不工作..getting error:Fatal error: Uncaught Error: Call to undefined function file_get_html()
    • 你必须下载它
    • 我已添加但无法正常工作..我没有获得所有 href 链接
    • 仅加载.php 文件。让我看看你在那做了什么
    • find('a') as $element) { $links[] = $element->href; } 回声 json_encode($links); ?>
    猜你喜欢
    • 2021-09-20
    • 2021-01-01
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-11
    • 1970-01-01
    • 2018-10-26
    相关资源
    最近更新 更多