php - 变量未找到网络爬虫 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 开发网络爬虫
解决方案
您可以使用:
if (strpos($url, "https://") == true)
代替
if (strpos($website_to_crawl, "https://") == true)
推荐阅读
- php - 需要 wordpress 登录才能查看自定义页面
- requirements - 在 IBM (Telelogic) DOORS 中插入一个空列
- spring - 监听 Spring Integration TCP Connection 事件
- php - 支持的方法:GET、HEAD 但不是 POST
- javascript - 记住浏览器后退按钮上最后选择的单选按钮
- arduino - STM32F103xxxx 在应用程序编程中使用 Arduino
- c# - 为什么我们在 C# 方法中需要多个 `await` 语句?
- vb.net - 在 VB.NET 应用程序中使用域名将文件从本地系统复制到在线服务器
- delphi - 无法加载 SSL 库
- algorithm - dijkstra 与 Bellman-Ford 算法