Crawl a website, get the links, crawl the links with PHP and XPATH

一曲冷凌霜 提交于 2019-11-29 04:25:42
Team Webgalli

You can try the following. See this thread for more details

<?php
//set_time_limit (0);
function crawl_page($url, $depth = 5){
$seen = array();
if(($depth == 0) or (in_array($url, $seen))){
    return;
}   
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec ($ch);
curl_close ($ch);
if( $result ){
    $stripped_file = strip_tags($result, "<a>");
    preg_match_all("/<a[\s]+[^>]*?href[\s]?=[\s\"\']+"."(.*?)[\"\']+.*?>"."([^<]+|.*?)?<\/a>/", $stripped_file, $matches, PREG_SET_ORDER ); 
    foreach($matches as $match){
        $href = $match[1];
            if (0 !== strpos($href, 'http')) {
                $path = '/' . ltrim($href, '/');
                if (extension_loaded('http')) {
                    $href = http_build_url($href , array('path' => $path));
                } else {
                    $parts = parse_url($href);
                    $href = $parts['scheme'] . '://';
                    if (isset($parts['user']) && isset($parts['pass'])) {
                        $href .= $parts['user'] . ':' . $parts['pass'] . '@';
                    }
                    $href .= $parts['host'];
                    if (isset($parts['port'])) {
                        $href .= ':' . $parts['port'];
                    }
                    $href .= $path;
                }
            }
            crawl_page($href, $depth - 1);
        }
}   
echo "Crawled {$href}";
}   
crawl_page("http://www.sitename.com/",3);
?>
$doc = new DOMDocument; 
$doc->load('file.htm'); 

$items = $doc->getElementsByTagName('a'); 

foreach($items as $value) { 
    echo $value->nodeValue . "\n"; 
    $attrs = $value->attributes; 
    echo $attrs->getNamedItem('href')->nodeValue . "\n"; 
}; 

find link from website recursively with depth

<?php


$depth = 1;

print_r(getList($depth));  


function getList($depth)  
{
    $lists = getDepth($depth);
    return $lists; 
 }

function getUrl($request_url)
{
    $countValid = 0;
    $brokenCount =0;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $request_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // We want to get the respone
    $result = curl_exec($ch);
    $regex = '|<a.*?href="(.*?)"|';
    preg_match_all($regex, $result, $parts);
    $links = $parts[1];
    $lists = array();
    foreach ($links as $link)
    {
        $url = htmlentities($link);
        $result =getFlag($url);
        if($result == true)
        {
            $UrlLists["clean"][$countValid] =$url;
            $countValid++; 
        } 
        else
        {
            $UrlLists["broken"][$brokenCount]= "broken->".$url;
            $brokenCount++;
        }  

    }
    curl_close($ch);
    return $UrlLists;
}
function ZeroDepth($list)
{
    $request_url = $list;
    $listss["0"]["0"] = getUrl($request_url);
    $lists["0"]["0"]["clean"] = array_unique($listss["0"]["0"]["clean"]);
    $lists["0"]["0"]["broken"] = array_unique($listss["0"]["0"]["broken"]);
    return $lists; 
}

function getDepth($depth)
{        
   // $list =OW_URL_HOME;
    $list = "https://example.com";//enter the url of website 
    $lists =ZeroDepth($list);
    for($i=1;$i<=$depth;$i++)
    {
        $l= $i;
        $l= $l-1;
        $depthArray=1;
        foreach($lists[$l][$l]["clean"] as $depthUrl)
        { 
            $request_url = $depthUrl;
            $lists[$i][$depthArray]["requst_url"]=$request_url;
            $lists[$i][$depthArray] = getUrl($request_url);

        }  

    }
    return $lists;   
}

function getFlag($url) 
{
    $url_response = array();
    $curl = curl_init();
    $curl_options = array();
    $curl_options[CURLOPT_RETURNTRANSFER] = true;
    $curl_options[CURLOPT_URL] = $url;
    $curl_options[CURLOPT_NOBODY] = true;
    $curl_options[CURLOPT_TIMEOUT] = 60;
    curl_setopt_array($curl, $curl_options);
    curl_exec($curl);
    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    if ($status == 200) 
    { 
        return true;
    } 
    else 
    {
        return false;
    }
    curl_close($curl);
}
?>` 

Please check the code below, hope it helps you.

<?php
$html = new DOMDocument();
@$html->loadHtmlFile('http://www.yourdomain.com');
$xpath = new DOMXPath( $html );
$nodelist = $xpath->query( "//div[@class='A-CLASS-Name']/h3/a/@href" );
foreach ($nodelist as $n){
    echo $n->nodeValue."\n<br>";
}
?>

Thanks, Roger

<?php
$path='http://www.hscripts.com/';
$html = file_get_contents($path);
$dom = new DOMDocument();
@$dom->loadHTML($html);
// grab all the on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");
for ($i = 0; $i < $hrefs->length; $i++ ) {
$href = $hrefs->item($i);
$url = $href->getAttribute('href');
echo $url.'<br />';
}
?>

you can use above code to get all possible links

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!