首页 > 解决方案 > 变量未找到网络爬虫 php

问题描述

我正在使用 php 开发网络爬虫,我总是遇到这个问题,但我没有找到解决它的方法

注意:未定义变量:第 16 行 C:\xampp\htdocs\dousser\index.php 中的 website_to_crawl

这是我的第二篇文章,没有显示错误但也没有显示任何内容

<?php 

$website_to_crawl= "http://php.net";
$all_links= array();


function get_links($url)

{
global $all_links;
$contents= @file_get_contents($url);
$regexp= "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
preg_match_all("/$regexp/siU", $contents, $matches);
$path_of_url= parse_url($url, PHP_URL_HOST);

if (strpos($url, "https://") == true)
{
$type= "https://";
}
else
{
$type= "http://";
}


$links_in_array= $matches[2];

foreach ($links_in_array as $link)

{

if (strpos($link, "#") !== false)
{
$link= substr($link,0, strpos($link, "#"));
}

if (substr($link, 0, 1) == ".")
{
$link= substr($link,1);
}

if (substr($link, 0, 7) == "http://") {
$link= $link;
}

else if (substr($link, 0, 8) == "https://") {
$link= $link;
}

else if (substr($link, 0, 2) == "//") {
$link= substr($link,2);
}



else if (substr($link, 0, 1) == "#") {
$link= $url;
}
else if (substr($link, 0, 7) == "mailto:") {
$link= "[" . $link . "]";
}
else if (substr($link, 0, 1) != "/") {
    $link= "$type" .$path_of_url. "/" . $link;
}
else 
{
$link= "$type" .$path_of_url.$link;
}


if (!in_array($link,$all_links))
{
array_push($all_links, $link);
}



}//ends foreach 

}//ends function get_links

get_links($website_to_crawl);

foreach ($all_links as $currentlink)
{
get_links($currentlink);
}

foreach ($all_links as $currentlink)
{

get_links($currentlink);
}

foreach ($all_links as $currentlink)
{
if ((strpos($currentlink, "www.php.net") !== FALSE) && (strpos($currentlink, "http", 4) == FALSE))
{
echo $currentlink . "<br>";
$linkscount[] += $currentlink;
}
}

$count= count($linkscount);

echo "<br><br>There are $count links found by the crawler";

?>

此变量用于 $website_to_crawl 网站进行爬网我已经检查过它是 http 还是 https 网站我已经测试过

提前致谢 在这种情况下,我正在使用网站 url 开发网络爬虫

标签: phpcloudflare

解决方案


您可以使用:

if (strpos($url, "https://") == true)

代替

if (strpos($website_to_crawl, "https://") == true)

推荐阅读