首页 > 解决方案 > 爬虫 php 应用程序 2018

问题描述

美好的一天,今天我向我的兄弟们询问“爬虫应用程序”,它需要包含以下部分:

这个任务真的是为了真正的 G :D

这是我到目前为止的代码:

<?php
function crawl_page($url, $depth = 5)
{

if (!isset($url) || $depth == 0) {
    return;
}


$dom = new DOMDocument('1.0');
@$dom->loadHTMLFile($url);

$anchors = $dom->getElementsByTagName('a');
foreach ($anchors as $element) {
    $href = $element->getAttribute('href');
    if (0 !== strpos($href, 'http')) {
        $path = '/' . ltrim($href, '/');
        if (extension_loaded('http')) {
            $href = http_build_url($url, array('path' => $path));
        } else {
            $parts = parse_url($url);
            $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 .= dirname($parts['path'], 1).$path;
        }
    }
    crawl_page($href, $depth - 1);
}
     echo "URL:".$url."<br />";
}
       crawl_page("http://www.pizza.com/", 2);

标签: phplaravel

解决方案


推荐阅读