准备
1、lumen(基于laravel改造,目前最快的API框架之一)
2、composer(php最好的依赖工具没有之一)
3、lumen下载好后,如何使用就自行看文档了,几乎和laravel使用方法一样(非常推荐该框架)
4、composer 下载好后,最好可以百度百度如何换镜像源,当然也可以不用换(只是下载速度会慢一些)
5、php >= 7.0
一、
打开lumen更目录使用composer下载两个包;
composer require niklaslu/php-sent-email //发送邮件包
composer require guzzlehttp/guzzle //http请求包
composer require jaeger/querylist //数据匹配包
二、
接下来就很简单了,使用http请求包 => 请求数据 => 数据匹配包 => 筛选数据 => 发送邮件
直接贴代码了;注意发送邮件包有一个小问题,就是他没有把字符集设置封装进去,自己改一下就可以了;
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use GuzzleHttp\Client;
use QL\QueryList;
use niklaslu\mail;
class DosCommand extends Command
{
/**
* 命令行执行命令
* @var string
*/
protected $signature = 'tel_reptile_1';
/**
* 命令描述
*
* @var string
*/
protected $description = 'this reptile 1';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
header("Content-type: text/html; charset=utf-8");
$limit = 10;
$page = 1;
ini_set('max_execution_time', '0');
set_time_limit(0); //执行时间无限
ini_set('memory_limit', '-1'); //内存无限
$are = [
'hunyuanxian','zuoyunxian','chengousan','kuangou','datongxian'
];
foreach($are as $v){
$this->wetch($limit, $page, "http://www.go007.com/datong/{$v}/fangwuchuzu/", $v);
}
}
public function unescape($str)
{
$ret = '';
$len = strlen($str);
for ($i = 0; $i < $len; $i ++)
{
if ($str[$i] == '%' && $str[$i + 1] == 'u')
{
$val = hexdec(substr($str, $i + 2, 4));
if ($val < 0x7f)
$ret .= chr($val);
else
if ($val < 0x800)
$ret .= chr(0xc0 | ($val >> 6)) .
chr(0x80 | ($val & 0x3f));
else
$ret .= chr(0xe0 | ($val >> 12)) .
chr(0x80 | (($val >> 6) & 0x3f)) .
chr(0x80 | ($val & 0x3f));
$i += 5;
} else
if ($str[$i] == '%')
{
$ret .= urldecode(substr($str, $i, 3));
$i += 2;
} else
$ret .= $str[$i];
}
return $ret;
}
public function wetch($limit, $page, $url, $are){
$i = $page;
$limit = $limit;
$client = new Client();
if(!file_exists("./{$are}fangwuchuzu.txt")){
$beijin = fopen("./{$are}fangwuchuzu.txt", "w");
}else{
$beijin = fopen("./{$are}fangwuchuzu.txt", "w");
}
for($i; $i <= $limit; $i++) {
$rules = array(
'url' => array('.f_l>li>h2>a', 'href'),
// 'wea' => array('.wea', 'text'),
// 'start_werther' => array('.tem>span', 'text'),
// 'end_werther' => array('.tem>i', 'text')
);
try {
$response = $client->get($url . "p{$i}");
$contents = $response->getBody()->getContents();
$url_1 = QueryList::html($contents)->rules($rules)->query()->getData();
}catch(\Exception $e){
echo $are."\r\n";
return 0;
}
foreach ($url_1 as $v) {
$rules = array(
'tel' => array('.viewad-meta>li>span>script', 'text'),
// 'wea' => array('.wea', 'text'),
// 'start_werther' => array('.tem>span', 'text'),
// 'end_werther' => array('.tem>i', 'text')
);
$response = $client->get($v['url']);
$contents = $response->getBody()->getContents();
$data = QueryList::html($contents)->rules($rules)->query()->getData();
if(!empty($data['1']['tel'])) {
$tel = $this->unescape($data['1']['tel']);
preg_match('/([0-9]{11})/', $tel, $a);
if (!empty($a['0'])) {
fwrite($beijin, $a['0'] . "\r\n");
}
}
usleep(1000);
}
echo $i."页\r\n";
usleep(1000);
}
echo $are."\r\n";
return;
}
}