2009-07-15 10 views
0

J'ai une page avec des URL avec des descriptions énumérées les unes sous les autres (quelque chose comme des signets/liste de sites). Comment puis-je utiliser php pour obtenir toutes les URL de cette page et les écrire dans un fichier txt (un par ligne, seulement une URL sans description)?Comment obtenir toutes les URL de la page (php)

page ressemble à ceci:

Some description

Other description

Another one

Et je voudrais la sortie du script txt pour ressembler à ceci:

http://link.com

http://link2.com

http://link3.com

Répondre

9

une façon

$url="http://wwww.somewhere.com"; 
$data=file_get_contents($url); 
$data = strip_tags($data,"<a>"); 
$d = preg_split("/<\/a>/",$data); 
foreach ($d as $k=>$u){ 
    if(strpos($u, "<a href=") !== FALSE){ 
     $u = preg_replace("/.*<a\s+href=\"/sm","",$u); 
     $u = preg_replace("/\".*/","",$u); 
     print $u."\n"; 
    } 
} 
2

Vous pouvez l'utiliser pour obtenir tout le lien dans la page Web donnée.

<?php 

    $var = fread_url($url); 

    preg_match_all ("/a[\s]+[^>]*?href[\s]?=[\s\"\']+". 
        "(.*?)[\"\']+.*?>"."([^<]+|.*?)?<\/a>/", 
        $var, &$matches); 

    $matches = $matches[1]; 
    $list = array(); 

    foreach($matches as $var) 
    {  
     print($var."<br>"); 
    } 

    function fread_url($url,$ref="") 
    { 
     if(function_exists("curl_init")){ 
      $ch = curl_init(); 
      $user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; ". 
          "Windows NT 5.0)"; 
      $ch = curl_init(); 
      curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); 
      curl_setopt($ch, CURLOPT_HTTPGET, 1); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION , 1); 
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION , 1); 
      curl_setopt($ch, CURLOPT_URL, $url); 
      curl_setopt($ch, CURLOPT_REFERER, $ref); 
      curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); 
      $html = curl_exec($ch); 
      curl_close($ch); 
     } 
     else{ 
      $hfile = fopen($url,"r"); 
      if($hfile){ 
       while(!feof($hfile)){ 
        $html.=fgets($hfile,1024); 
       } 
      } 
     } 
     return $html; 
    } 

    ?> 
Questions connexes