PHP 爬虫爬取社区文章内容

php也可以写爬虫

  • 说起爬虫,大多数第一反应都是python,python强大的requests和bs4等等强大的第三方库让人们都喜欢用python去写爬虫。但是php作为“世界上最好的语言”当然也可以用来开发爬虫。
  • 写了一个小的爬虫爬取社区的文章源码地址

前期准备

  • composer
  • Guzzle Guzzle是一个十分强大的php的模拟HTTP client的第三方库,可以通过composer安装
  • Goutte Goutte是一个用来解析HTML文档的第三方库,可以通过composer安装

开始工作

1.安装两个库

  • Goutte composer require fabpot/goutte
  • Guzzle composer require guzzlehttp/guzzle:~6.0

2.创建命令

  • php artisan make:command Spider

3.命令参数

  • protected $signature = 'command:spider {concurrency} {keyWords*}'; //concurrency为并发数 keyWords为查询关键词

4.编写爬虫

  • 写了一个简单的,主要用来爬取社区的文章,通过命令行参数获取要搜索的关键词,然后爬取文章,并爬下内容存在本地。直接贴代码啦。
<?php

namespace App\Console\Commands;

use Goutte\Client as GoutteClient;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Pool;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;

class Spider extends Command
{

    protected $signature = 'command:spider {concurrency} {keyWords*}'; //concurrency为并发数  keyWords为查询关键词

    protected $description = 'php spider';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        //
        $concurrency = $this->argument('concurrency');  //并发数
        $keyWords = $this->argument('keyWords');    //查询关键词
        $guzzleClent = new GuzzleClient();
        $client = new GoutteClient();
        $client->setClient($guzzleClent);
        $request = function ($total) use ($client,$keyWords){
            foreach ($keyWords as $key){
                $url='https://laravel-china.org/search?q='.$key;
                yield function () use($client,$url){
                    return $client->request('GET',$url);
                };
            }
        };
        $pool = new Pool($guzzleClent,$request(count($keyWords)),[
            'concurrency' => $concurrency,
            'fulfilled' => function ($response, $index) use ($client){
                $response->filter('h2 > a')->reduce(function($node) use ($client){
                    if(strlen($node->attr('title'))==0) {
                        $title = $node->text();             //文章标题
                        $link = $node->attr('href');        //文章链接
                        $carwler = $client->request('GET',$link);       //进入文章
                        $content=$carwler->filter('#emojify')->first()->text();     //获取内容
                        Storage::disk('local')->put($title,$content);           //储存在本地
                    }
                });
            },
            'rejected' => function ($reason, $index){
                $this->error("Error is ".$reason);
            }
        ]);
        //开始爬取
        $promise = $pool->promise();
        $promise->wait();
    }
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
本帖由系统于 6年前 自动加精
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 3

是不是少了东西

6年前 评论
sushengbuhuo

报错了

$ php artisan command:spider 5 guzzle

In Local.php line 129:

  file_put_contents(): failed to open stream: Invalid argument
5年前 评论

是否支持html页面爬取?

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!