PHP code example of l1n6yun / crawler

1. Go to this page and download the library: Download l1n6yun/crawler library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

l1n6yun / crawler example snippets




use Crawler\Crawler;

// 设置线程数
$threads = 10;
// 设置起始页和结束页
$startPage = 1;
$endPage = 100;

// MongoDB 数据库连接信息
$mongoDbUri = 'mongodb://localhost:27017/';
$mongoDbName = 'blog';
$mongoDbCollection = 'article';

// 创建 Crawler 实例
$crawler = new Crawler($mongoDbUri, $mongoDbName, $mongoDbCollection);

// 抓取列表
$crawler->list($threads, function ($page) {
    // 在这里实现抓取列表的逻辑...
    // 例如,访问一个 URL 并解析页面,然后返回数据数组
    return [
        [
            'title' => '页面标题', // 页面标题
            'link' => '页面链接', // 页面链接
            '_id' => md5('页面链接'), // 使用链接的 MD5 值作为唯一标识
        ],
    ];
}, $endPage, $startPage);

// 抓取详情
$crawler->detail($threads, function ($doc) {
    // 在这里实现抓取详情的逻辑...
    // 例如,访问详情页并解析内容,然后返回数据数组
    return [
        'content' => '页面内容', // 页面内容
    ];
});

// 查询爬虫状态
$status = $crawler->status();