基于 Yii 2 像 jQuery 一样使用选择器在HTML页面上查找标签

1、HTML的数据结构如下,现在需要提取出:视频描述。。。。,即第一个p标签中的数据

<!--PLAYERCODESTART-->
<div id="4ce305d265eb4cd7a17db4c4b09ae1d8"></div>
<script type="text/javascript">var vmsPlayer_callback = function () {createPlayer("video://vid:166bc1bf143c445a9e6ad0ce0565b517","%3Fguid%3D166bc1bf143c445a9e6ad0ce0565b517%26siteId%3D2","2018-03-13","@WIDTH@","@HEIGHT@")};</script>
<script type="text/javascript" src="http://wjmserver.chinamcloud.com/vms/site/kfcs/media/playerJson/vod/4ce305d265eb4cd7a17db4c4b09ae1d8.js"></script>
<!--PLAYERCODEEND-->
<div style="font-size:18px;line-height:1.5;" id="editWrap">
    <p>视频描述。。。。</p>
    <p style="text-align: center;">
        <img border="0" style="max-width:100%;" zcmsimagerela="7734" src="http://wjtest.sobeycloud.com/cms/ptu//upload/Image/mrtp/1_6e70e22f7bb447b6a4e75a2979fb4516.jpg?1520927692325" alt="1_640_400"/>
    </p>
    <p>
        <br/>
    </p>
</div>

2、在 github 上搜索:yii2 html dom,最终选择:garyjl/yii2-simple_html_dom,如图1

在 github 上搜索:yii2 html dom,最终选择:garyjl/yii2-simple_html_dom

图1

3、执行命令:composer require –prefer-dist garyjl/yii2-simple_html_dom “*”,安装扩展,安装失败,如图2

执行命令:composer require --prefer-dist garyjl/yii2-simple_html_dom "*",安装扩展,安装失败

图2

but these conflict with your requirements or minimum-stability.

4、编辑 composer.json,新增:”garyjl/yii2-simple_html_dom”: “*@dev”,设置支持稳定性标签 @dev,如图3

"require": {
    "php": ">=5.6.0",
    "ext-intl": "*",
    "yiisoft/yii2": "^2.0.6",
    "yiisoft/yii2-bootstrap": "^2.0.0",
    "yiisoft/yii2-swiftmailer": "^2.0.0",
    "yiisoft/yii2-authclient": "^2.0.0",
    "yiisoft/yii2-jui": "^2.0.0",
    "asofter/yii2-imperavi-redactor": "dev-master@dev",
    "mihaildev/yii2-elfinder": "^1.0",
    "trntv/yii2-aceeditor": "^2.0",
    "trntv/probe": "^0.2",
    "trntv/yii2-file-kit": "^1.0.0",
    "trntv/yii2-glide": "^1.0.0",
    "trntv/yii2-datetime-widget": "^1.0.0",
    "trntv/cheatsheet": "^0.1@dev",
    "trntv/yii2-command-bus": "^2.0",
    "intervention/image": "^2.1",
    "vlucas/phpdotenv": "^2.0",
    "bower-asset/admin-lte": "^2.0",
    "bower-asset/font-awesome": "^4.0",
    "bower-asset/html5shiv": "^3.0",
    "bower-asset/jquery-slimscroll": "^1.3",
    "bower-asset/flot": "^0.8",
    "symfony/process": "^3.0",
    "yiisoft/yii2-redis": "^2.0",
    "hprose/hprose-yii": "^2.0",
    "garyjl/yii2-simple_html_dom": "*@dev"
  },
编辑 composer.json,新增:"garyjl/yii2-simple_html_dom": "*@dev",设置支持稳定性标签 @dev

图3

5、运行命令:composer update,执行成功,如图4

运行命令:composer update,执行成功

图4

6、内容类型:视频,则取出$content[‘content’]的第一个p标签的数据

use garyjl/simplehtmldom/SimpleHtmlDom;

$html = SimpleHtmlDom::str_get_html($content['content']);
$content = $html->find('p', 0)->text();
print_r($content);
exit;

7、打印结果,符合预期,如图5

视频描述。。。。
打印结果,符合预期

图5

8、升级至生产环境,编辑 .env,调整环境为生产环境

YII_DEBUG   = false
YII_ENV     = prod

9、运行程序,报错,查看:/api/runtime/logs/app.log

2018-03-20 11:04:20 [api][/v1/content-audits?tenantid=default][error][Error] Error: Class 'garyjl/simplehtmldom/SimpleHtmlDom' not found in /sobey/www/cmcp-api/api/models/ContentAudit.php:437
Stack trace:
#0 /sobey/www/cmcp-api/api/rests/content_audit/CreateAction.php(55): api/models/ContentAudit->create()
#1 [internal function]: api/rests/content_audit/CreateAction->run()
#2 /sobey/www/cmcp-api/vendor/yiisoft/yii2/base/Action.php(94): call_user_func_array(Array, Array)
#3 /sobey/www/cmcp-api/vendor/yiisoft/yii2/base/Controller.php(157): yii/base/Action->runWithParams(Array)
#4 /sobey/www/cmcp-api/vendor/yiisoft/yii2/base/Module.php(528): yii/base/Controller->runAction('create', Array)
#5 /sobey/www/cmcp-api/vendor/yiisoft/yii2/web/Application.php(103): yii/base/Module->runAction('v1/content-audi...', Array)
#6 /sobey/www/cmcp-api/vendor/yiisoft/yii2/base/Application.php(386): yii/web/Application->handleRequest(Object(yii/web/Request))
#7 /sobey/www/cmcp-api/api/web/index.php(23): yii/base/Application->run()
#8 {main}
2018-03-20 11:04:20 [api][/v1/content-audits?tenantid=default][warning][yii/log/Dispatcher::dispatch] Unable to send log via yii/log/EmailTarget: Exception 'Swift_TransportException' with message 'Expected response code 220 but got code "", with message ""'

in /sobey/www/cmcp-api/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php:419

Stack trace:
#0 /sobey/www/cmcp-api/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(325): Swift_Transport_AbstractSmtpTransport->assertResponseCode('', Array)
#1 /sobey/www/cmcp-api/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(130): Swift_Transport_AbstractSmtpTransport->readGreeting()
#2 /sobey/www/cmcp-api/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/SendmailTransport.php(52): Swift_Transport_AbstractSmtpTransport->start()
#3 /sobey/www/cmcp-api/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mailer.php(67): Swift_Transport_SendmailTransport->start()
#4 /sobey/www/cmcp-api/vendor/yiisoft/yii2-swiftmailer/Mailer.php(150): Swift_Mailer->send(Object(Swift_Message))
#5 /sobey/www/cmcp-api/vendor/yiisoft/yii2/mail/BaseMailer.php(263): yii/swiftmailer/Mailer->sendMessage(Object(yii/swiftmailer/Message))
#6 /sobey/www/cmcp-api/vendor/yiisoft/yii2/mail/BaseMessage.php(49): yii/mail/BaseMailer->send(Object(yii/swiftmailer/Message))
#7 /sobey/www/cmcp-api/vendor/yiisoft/yii2/log/EmailTarget.php(88): yii/mail/BaseMessage->send(Object(yii/swiftmailer/Mailer))
#8 /sobey/www/cmcp-api/vendor/yiisoft/yii2/log/Target.php(133): yii/log/EmailTarget->export()
#9 /sobey/www/cmcp-api/vendor/yiisoft/yii2/log/Dispatcher.php(189): yii/log/Target->collect(Array, true)
#10 /sobey/www/cmcp-api/vendor/yiisoft/yii2/log/Logger.php(177): yii/log/Dispatcher->dispatch(Array, true)
#11 /sobey/www/cmcp-api/vendor/yiisoft/yii2/base/ErrorHandler.php(113): yii/log/Logger->flush(true)
#12 [internal function]: yii/base/ErrorHandler->handleException(Object(Error))
#13 {main}

10、查看 Git,发现 /vendor/garyjl/yii2-simple_html_dom 目录下为空,如图6

查看 Git,发现 /vendor/garyjl/yii2-simple_html_dom 目录下为空

图6

11、删除并添加至忽略列表:/vendor/garyjl/yii2-simple_html_dom,如图7

删除并添加至忽略列表:/vendor/garyjl/yii2-simple_html_dom

图7

12、编辑.gitignore,删除一行:/vendor/garyjl/yii2-simple_html_dom,保存,如图8

编辑.gitignore,删除一行:/vendor/garyjl/yii2-simple_html_dom,保存

图8

13、删除目录:/vendor/garyjl/yii2-simple_html_dom/.git,如图9

删除目录:/vendor/garyjl/yii2-simple_html_dom/.git

图9

14、新增:/vendor/garyjl/yii2-simple_html_dom,如图10

新增:/vendor/garyjl/yii2-simple_html_dom

图10

15、查看 /common/config/base.php

$config = [
    'components' => [
        'mailer' => [
            'class' => 'yii/swiftmailer/Mailer',
            //'useFileTransport' => true,
            'messageConfig' => [
                'charset' => 'UTF-8',
                'from' => env('ADMIN_EMAIL')
            ]
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                'file'=>[
                    'class' => 'yii/log/FileTarget',
                    'levels' => ['error', 'warning'],
                    'except' => ['yii/web/HttpException:*', 'yii/i18n/I18N/*'],
                    'prefix' => function () {
                        $url = !Yii::$app->request->isConsoleRequest ? Yii::$app->request->getUrl() : null;
                        return sprintf('[%s][%s]', Yii::$app->id, $url);
                    },
                    'logVars'=>[],
                ],
                'httpRequest'=>[
                    'class' => 'yii/log/FileTarget',
                    'logFile' => '@runtime/logs/http-request.log',
                    'categories' => ['yii/httpclient/*'],
                ]
            ],
        ],
    ],
];
if (YII_ENV_PROD) {
    $config['components']['log']['targets']['email'] = [
        'class' => 'yii/log/EmailTarget',
        'except' => ['yii/web/HttpException:*'],
        'levels' => ['error', 'warning'],
        'message' => ['from' => env('ROBOT_EMAIL'), 'to' => env('ADMIN_EMAIL')]
    ];
}

16、注释日志目标:[[yii/log/EmailTarget|email target]],编辑 /common/config/base.php

/*
if (YII_ENV_PROD) {
    $config['components']['log']['targets']['email'] = [
        'class' => 'yii/log/EmailTarget',
        'except' => ['yii/web/HttpException:*'],
        'levels' => ['error', 'warning'],
        'message' => ['from' => env('ROBOT_EMAIL'), 'to' => env('ADMIN_EMAIL')]
    ];
}
*/

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/250399.html

(0)
上一篇 2022年4月29日
下一篇 2022年4月29日

相关推荐

发表回复

登录后才能评论