您现在的位置是:首页 > PHP框架交流PHP框架交流
thinkphp使用querylist采集笑话网站笔记
上善若水2020-07-15 00:34:19【PHP框架交流】
3545人已围观
简介thinkphp使用querylist采集笑话网站笔记,使用thinkphp commands命令写法采集小说数据,不多说直接上代码. 1.创建数据采集记录表CREATE TABLE `joke_li
thinkphp使用querylist采集笑话网站笔记,使用thinkphp commands命令写法采集小说数据,不多说直接上代码.
1.创建数据采集记录表
CREATE TABLE `joke_list` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`category` varchar(255) DEFAULT NULL,
`title` varchar(255) DEFAULT NULL,
`link` varchar(255) DEFAULT NULL,
`status` tinyint(4) DEFAULT '1',
`create_time` datetime DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `joke_detail` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`category` varchar(255) DEFAULT NULL,
`title` varchar(255) DEFAULT NULL,
`tag` varchar(255) DEFAULT NULL,
`description` varchar(2000) DEFAULT NULL,
`content` text,
`link` varchar(255) DEFAULT NULL,
`status` tinyint(4) DEFAULT '1',
`create_time` datetime DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
2.编写数据库模型
JokeDetail.php
<?php
declare (strict_types = 1);
namespace app\model;
use think\Model;
/**
* @mixin \think\Model
*/
class JokeDetail extends Model
{
protected $autoWriteTimestamp = 'datetime';
}
JokeList.php
<?php
declare (strict_types = 1);
namespace app\model;
use think\Model;
/**
* @mixin \think\Model
*/
class JokeList extends Model
{
protected $autoWriteTimestamp = 'datetime';
}
3.安装querylist框架
执行composer安装命令composer require jaeger/querylist
参考安装文档
4.创建commands编写采集脚本代码
<?php
declare (strict_types=1);
namespace app\command;
use app\model\JokeDetail;
use app\model\JokeList;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use QL\QueryList;
use QL\Ext\AbsoluteUrl;
use GuzzleHttp\Exception\RequestException;
use think\facade\Log;
class Joke extends Command
{
protected function configure()
{
// 指令配置
$this->setName('caiji')
->setDescription('the caiji command');
}
protected function execute(Input $input, Output $output)
{
$this->task();
$this->task2();
}
protected function task()
{
$domain = "https://xiaohua.bbwx.com/";
$ql = QueryList::getInstance();
$ql->use(AbsoluteUrl::class);
$rules = array(
'category' => ['header>a', 'text'],
'link' => ['h2 a', 'href'],
'title' => ['h2 a', 'text']
);
// 切片选择器
$range = ".content .excerpt-text";
$i = 1;
while (true) {
try {
$q1 = $ql->get("https://xiaohua.bbwx.com/page/{$i}", null, ["timeout" => 30])->absoluteUrl($domain);
$rt1 = $q1->rules($rules)
->range($range)->query()->getData()->all();
Log::INFO("开始采集第{$i}页数据!");
if ($rt1) {
$jokeList = new JokeList();
$jokeList->saveAll($rt1);
}
} catch (RequestException $e) {
Log::INFO("第一页数据请求超时,正在重试!");
continue;
}
if ($i >= 146) {
break;
}
$i++;
};
}
protected function task2()
{
$ql = QueryList::getInstance();
$jokeList = JokeList::where("status", 1)->select();
foreach ($jokeList as $joke) {
try {
$q1 = $ql->get($joke->link, null, ["timeout" => 30]);
$q1->find('.content .article-content .asb')->remove();
$joke_detail = [
"category"=> $joke->category,
"title"=> $ql->find('.article-title')->text(),
"tag"=>$ql->find(".article-tags>a")->text(),
"description"=>$ql->find("meta[name='description']")->attr("content"),
"content"=>htmlentities($ql->find('.content .article-content')->html()),
"link"=>$joke->link,
"status"=>2
];
$jokeDetail=JokeDetail::where("link",$joke->link)->findOrEmpty();
$jokeDetail->save($joke_detail);
$joke->status=2;
$joke->save();
} catch (RequestException $e) {
Log::INFO("请求超时{$joke->link},正在重试!");
continue;
}
}
}
}
5.注册该脚本命令config/console.php
<?php
// +----------------------------------------------------------------------
// | 控制台配置
// +----------------------------------------------------------------------
return [
// 指令定义
'commands' => [
'joke' => 'app\command\Joke',
],
];
6.执行命令采集结果等待采集完成PHP think joke
Tags: querylist
很赞哦! (6)
相关文章
随机图文
一个让人瞬间爆笑的笑话
1、有一个女孩子平常被妈妈管的很严。有一次被男朋友叫去看电影,临出门时妈妈嘱咐说:“出去要放聪明点不要被男人占了便宜,如果他摸你上面你就说不要,摸你下边你就说停。”女孩说记住了,晚上回来她妈问她有没有被占便宜,女孩哭着说:“占了,他上下一起摸我,我就照你教的说:不要停,不要停。 2、幸福就是痒的时候挠一下,不幸就是痒了但挠不着,更不幸的是,很久以来灵魂和肉体都感觉不到那种蠢蠢欲动的痒了。 3、java转xml maven扩展包推荐 比XStream与JAXB更好的xml方式
最近在做java转xml时遇到了一些问题,在百度上搜索可用的jar时,首先搜索到的是JAXB放时,这种方式我遇到的两个问题,后来换成XStream,还行,直到我在maven仓库中找到了jackson-csv设置utf8格式 导入csv乱码记事本转utf8格式 无须office wps其他软件
我们在项目开发的时候经常会使用csv用来导入导出数据,打开csv的时候文件显示正常,但是遇到特殊符号时,显示乱码,我们一般在php里手动进行转码,有时候比较麻烦,我们直接将原csv转换成utf8格式就spriingboot之mybatis 一对多关联查询
在接触mybatis时,只会使用mapper提供的单表查询,关联查询需要配置xml完成,整理了使用@Results注解完成表的关联查询.参考博客:https://blog.csdn.net/zhous