吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2416|回复: 8
收起左侧

[其他原创] PHP 多线程爬取天涯帖子

[复制链接]
fengxiaoxiao7 发表于 2022-12-19 15:28

使用了querylist框架来解析dom

话不多说,上代码

<?php

namespace app\command\crawler;

use GuzzleHttp\Psr7\Response;
use QL\QueryList;

class Tianya
{
    protected $url = "";// 帖子地址

    protected $title;
    protected $author;
    protected $urls = [];
    protected $mainPosts;
    protected $posts;

    public function execute(){
        $html = QueryList::get($this->url);
        $this->title = $html->find(".s_title")->text();
        $this->author = $this->handlerSymbol($html->find(".atl-info:eq(0)")->text());
        $pages = $html->find(".atl-pages:eq(0) a")->texts()->filter(function ($item){
            return is_numeric($item);
        })->all();

        if(!empty($pages)){
            $maxPage =  max($pages);
            $this->getUrls($maxPage);
        }
        $this->mainPosts = $html->rules([
            'author' => [".atl-info","text"],
            'content' => [".bbs-content","text"],
            'comments' => [".ir-list li","texts","-.ir-power -.ir-reply"],
        ])->range(".atl-item")->query()->getData()->all();
        $this->writeFile();
        $this->crawlOtherPage();
    }

    protected function getUrls($maxPage){
        if(is_numeric($maxPage)){
            for ($i=2;$i<=$maxPage;$i++){
                $this->urls[] =  str_replace("-1.shtml", "-{$i}.shtml", $this->url);
            }
        }
    }

    protected function writeFile(){
        $file = "{$this->title}.txt";
        if(is_file($file)){
            unlink($file);
        }
        $handler = fopen($file, "a+");
        fwrite($handler,$this->title.PHP_EOL);
        fwrite($handler,$this->author.PHP_EOL);
        foreach ($this->mainPosts as $item){
            fwrite($handler,$this->writeString($item['author']));
            fwrite($handler,$this->writeString($item['content']));
            fwrite($handler,$this->writeComment($item['comments']));
        }
        fclose($handler);
    }

    protected function writePosts(){
        $file = "{$this->title}.txt";
        $handler = fopen($file, "a+");
        sort($this->posts);
        foreach ($this->posts as $page){
            foreach ($page as $item){
                fwrite($handler,$this->writeString($item['author']));
                fwrite($handler,$this->writeString($item['content']));
                fwrite($handler,$this->writeComment($item['comments']));
            }
        }
        fclose($handler);
    }

    protected function crawlOtherPage(){
        if(!empty($this->urls)){
            $rules = [
                'author' => ['.atl-info','text'],
                'content' => ['.bbs-content','text'],
                'comments' => [".ir-list li","texts","-.ir-power -.ir-reply"],
            ];
            $range = '.atl-main .atl-item';
            QueryList::rules($rules)
                ->range($range)
                ->multiGet($this->urls)
                // 设置并发数为5
                ->concurrency(5)
                // 设置GuzzleHttp的一些其他选项
                ->withOptions([
                    'timeout' => 60
                ])
                // 设置HTTP Header
                ->withHeaders([
                    'User-Agent' => 'QueryList'
                ])
                // HTTP success回调函数
                ->success(function (QueryList $ql, Response $response, $index){
                    $data = $ql->queryData();
                    $this->posts[$index] = $data;
                })
                // HTTP error回调函数
                ->error(function (QueryList $ql, $reason, $index){
                    // ...
                })
                ->send();

            $this->writePosts();
        }

    }

    protected function handlerSymbol($string){
        return preg_replace('/\s+/', '   ',$string);
    }

    protected function writeString($string){
        return $this->handlerSymbol($string).PHP_EOL;
    }

    protected function writeComment($comments){
        $string = "";
        if($comments){
            $string.="评论:".PHP_EOL;
        }
        if(!empty($comments) && is_array($comments)){
            foreach ($comments  as $comment){
                $string .= "    ".$this->handlerSymbol($comment).PHP_EOL;
            }
        }

        $string.="\n\n\n\n\n";
        return $string;
    }
}

免费评分

参与人数 3吾爱币 +5 热心值 +3 收起 理由
wyxdh + 1 用心讨论,共获提升!
Bob5230 + 1 谢谢@Thanks!
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

素问何问 发表于 2022-12-19 18:03
QueryList 库不好用 而且很久没更新了
 楼主| fengxiaoxiao7 发表于 2022-12-19 18:06
素问何问 发表于 2022-12-19 18:03
QueryList 库不好用 而且很久没更新了

还行,我觉得算是php里面最好用的一款爬虫框架了
Selchar 发表于 2022-12-19 18:45
alongzhenggang 发表于 2022-12-19 19:23
看到这个  是不是和抓取是一个意思呢、、

我抓了一个网站   直接打不开了  

可有办法解决
 楼主| fengxiaoxiao7 发表于 2022-12-20 09:47
alongzhenggang 发表于 2022-12-19 19:23
看到这个  是不是和抓取是一个意思呢、、

我抓了一个网站   直接打不开了  

可能ip被封了吧,换个ip再试试
ddptsc 发表于 2022-12-21 11:21
php写爬虫台鸡肋了,基本没什么插件,爬虫还是py好用
apull 发表于 2022-12-23 20:36
多谢分享,学习了。
叶秋992 发表于 2022-12-24 10:23

多谢分享,学习了。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-24 09:31

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表