PHP简单爬虫CURL +Crawler 抓取取考研单词数据脚本

记录一个简单的爬虫功能,用来自己学习背诵单词。如果有侵权,立马删除。

目标是爬取一个网站的考研词汇。网址为:https://www.kuakao.com/english/ch/39243.html

首先在 在larvel框架中,生成一个artisan 脚本

我的代码:

填写数据:

开始写代码:

<?php

namespace App\Console\Commands;

use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Symfony\Component\DomCrawler\Crawler;

class FetchEnglishNote extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'fetch:englishNote';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '抓取一下单词,这是临时脚本';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $client = new Client();
        //一共62个页面

        for ($number = 39184; $number < 39244; $number++) {
            try {
                $url = 'https://www.kuakao.com/english/ch/' . $number . '.html';
                $response = $client->request('GET', $url);
                //转为文本形式
                $body = $response->getBody()->getContents();
                //Crawler是一个 包,可以以选择器的形式来抓取 数据,比较方便。
                $crawler = new Crawler($body);
                //通过 class属性 加P标签,然后拿到每一个元素
                $english = $crawler->filter('.artTxt p')->each(function (Crawler $node, $i) {
                    return $node->text() . PHP_EOL;
                });
                //打印下看下效果
                dd($english);
                file_put_contents(base_path() . '/public/111.txt', $english);

            } catch (\Exception $e) {
                echo $number . '出错' . $e->getMessage() . $e->getLine();
                die;
                continue;
            }

        }

    }
}

运行结果:比较幸运,直接拿到了一个文件的。然后把它们放在一个TXT文件中。

然后整体运行,发现太慢了,也不知道执行到什么地方了,所以加个进度条吧。

最终代码:

<?php

namespace App\Console\Commands;

use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Symfony\Component\DomCrawler\Crawler;

class FetchEnglishNote extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'fetch:englishNote';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '抓取一下单词,这是临时脚本';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $client = new Client();
        //一共62个页面
        $bar = $this->output->createProgressBar(62);
        $bar->start();
        for ($number = 39184; $number < 39244; $number++) {
            try {
                $url = 'https://www.kuakao.com/english/ch/' . $number . '.html';
                $response = $client->request('GET', $url);
                //转为文本形式
                $body = $response->getBody()->getContents();
                //Crawler是一个 包,可以以选择器的形式来抓取 数据,比较方便。
                $crawler = new Crawler($body);
                //通过 class属性 加P标签,然后拿到每一个元素
                $english = $crawler->filter('.artTxt p')->each(function (Crawler $node, $i) {
                    return $node->text() . PHP_EOL;
                });
                //打印下看下效果
                foreach($english as $oneWord){
                    file_put_contents(base_path() . '/public/englishNote.txt', $oneWord.'\n',FILE_APPEND);
                }

            } catch (\Exception $e) {
                echo $number . '出错' . $e->getMessage() . $e->getLine();
                die;
                continue;
            }
            $bar->advance();

        }
        $bar->finish();
        echo '恭喜全部完成';die;

    }
}

看下效果吧:

看来\n,我加的多余了,让各位大神耻笑了,我会继续加油。有什么问题欢迎大家留言。

猜你喜欢

转载自blog.csdn.net/wuye_lh/article/details/105511373
今日推荐