php - 如何设置 PHP Spatie Crawler - 输出结果
问题描述
我正在尝试设置Spatie,一个 PHP 爬虫,但很难解释文档。代码看起来相当健壮,但由于缺乏明确的路径来“这是如何在不做太多假设的情况下获得工作示例”,文档似乎存在一些非常基本的差距。
也就是说,我一直在阅读一堆其他 GitHub 线程和文章,试图让事情至少“更接近”设置。
我做了什么
- 我终于把事情弄到了没有因未导入的类等而导致的任何错误的地步
我被困在哪里
- 现在我有了一些代码,我不清楚实际发生了什么,我知道这听起来很奇怪
- 我期望的是某种 1)循环 2)在抓取时输出 URL 的方法 3)输出结果的方法
- 具体来说,作为一个用例,我想要一个 URL 列表及其各自的状态代码
任何有关我所缺少的见解的见解都将不胜感激。
我的代码:
use Spatie\Crawler\Crawler;
use Spatie\Crawler\CrawlObservers;
use Spatie\Crawler\CrawlObservers\CrawlObserver; // I had to specify this namespace, without it I kept getting an Exception: Class 'CrawlObserver' not found error
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface; // If I don't set this, I get an error: " Could not check compatibility between myClassExtendingCrawlObserver..."
class myClassExtendingCrawlObserver extends CrawlObserver {
/**
* Called when the crawler will crawl the url.
*
* @param \Psr\Http\Message\UriInterface $url
*/
public function willCrawl(UriInterface $url)
{
}
/**
* Called when the crawler has crawled the given url successfully.
*
* @param \Psr\Http\Message\UriInterface $url
* @param \Psr\Http\Message\ResponseInterface $response
* @param \Psr\Http\Message\UriInterface|null $foundOnUrl
*/
public function crawled(
UriInterface $url,
ResponseInterface $response,
?UriInterface $foundOnUrl = null
){
}
/**
* Called when the crawler had a problem crawling the given url.
*
* @param \Psr\Http\Message\UriInterface $url
* @param \GuzzleHttp\Exception\RequestException $requestException
* @param \Psr\Http\Message\UriInterface|null $foundOnUrl
*/
public function crawlFailed(
UriInterface $url,
RequestException $requestException,
?UriInterface $foundOnUrl = null
){
}
/**
* Called when the crawl has ended.
*/
public function finishedCrawling()
{
}
}
if(!class_exists('Spatie\\Crawler\\CrawlObservers\\CrawlObserver')){ // I was using this to check what to include
$myClassExtendingCrawlObserver = new myClassExtendingCrawlObserver();
$url = 'https://www.example.com';
try {
Crawler::create()
->setCrawlObserver($myClassExtendingCrawlObserver)
->startCrawling($url);
} catch (exception $e) {
error_log(e);
}
}
解决方案
Spatie Crawler 循环遍历 URL 中的链接,并返回状态和其他信息。您可以通过以下方式获取更多信息:
public function crawled(
UriInterface $url,
ResponseInterface $response,
?UriInterface $foundOnUrl = null
): void
{
echo 'Crawling URL: ' . urldecode($url) . ' ... ' . PHP_EOL;
echo 'Crawl result: ' . $response->getStatusCode() . ' - ' . $response->getReasonPhrase() . PHP_EOL;
if (isset($response->getHeaders()['Server'])) {
echo 'Server: ' . $response->getHeaders()['Server'][0] . PHP_EOL;
}
if (isset($response->getHeaders()['Set-Cookie'])) {
// You can use loop here
echo 'Cookies: ' . $response->getHeaders()['Set-Cookie'][0] . PHP_EOL;
}
if ($response->getStatusCode() == 301 || $response->getStatusCode() == 302) {
echo $response->getHeaders()['Location'][0] . PHP_EOL;
echo "Redirect: " . rtrim($url, '/') . $response->getHeaders()['Location'][0] . PHP_EOL;
}
}
您可以在此处处理失败的请求:
public function crawlFailed(
UriInterface $url,
RequestException $requestException,
?UriInterface $foundOnUrl = null
): void
{
echo '!!! Crawl Failed !!! : ' . $url . PHP_EOL;
}
推荐阅读
- arrays - 在 TypeScipt 中按字母字符搜索人?
- node.js - PDF-lib 生成的 dataUri 显示:服务器响应状态为 431
- r - 为什么 AUC 仅略有改善,而分类预测变量的影响却相当显着?
- spring-boot - 在抛出异常之前拦截对不正确正文类型的请求并记录请求正文
- javascript - 如何使 Vue 3 插件属性具有反应性?
- boto3 - (cdk + boto3):在 CDK 应用程序中使用 boto3
- sharepoint - 将文件从 Blazor Server 应用上传到 SharePoint Online 库
- javascript - 我处于无限循环中
- file - 如何通过`openstack application credential create`或identity api application_credentials获取openrc文件
- visual-studio-code - 如何访问 extension.ts 中的函数结果?VS 代码扩展