php - 爬虫 php 应用程序 2018
问题描述
美好的一天,今天我向我的兄弟们询问“爬虫应用程序”,它需要包含以下部分:
将结果保存在数据库中
前面将获得任何给定网站的所有链接,并在彼此下方建立索引
如果爬虫的结果太长,那么通过计算 URL 中的最后一个数字来移动到下一页,并再次获取如下所述的链接
首先获取在其 URL 中具有 «&id=» 的链接,然后再获取另一个
这个任务真的是为了真正的 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);
解决方案
推荐阅读
- assembly - x86 指令是否需要它们自己的编码以及它们的所有参数同时存在于内存中?
- python - AttributeError:“pygame.Rect”对象在进行碰撞时没有属性“rect”错误
- angular - '' 类型的 Babylonjs 参数不可分配给 '' 类型的参数
- javascript - 如何专注于标签,在Angular 8中点击通知?
- ios - 为什么在启动图像中选择“iOS 8.0 及更高版本”并为此新建图像后,iOS 应用程序的屏幕尺寸会发生变化?
- python - 是否可以使用带有“导入环境”菜单选项的 environment.yml Anaconda Navigator 更新我的环境?
- java - iText 7 使用具有最小宽度和最大宽度功能的相同 CSS 样式表生成具有两种页面大小的报告
- react-native - 使用 npm 在 Ubuntu 中安装 Expo-cli 的问题
- asp.net-mvc - 我可以在 ASP.NET Core 的中间件期间访问数据库吗?
- python - 为什么将列表存储在 Pandas 数据框中的单元格中是一个坏主意?