什么是毒汤日历
毒汤日历是一本有毒的日历,每天用毒鸡汤来唤醒你。
你甚至不用打开日历,打开 App 的推送,每天会定时送上一杯毒鸡汤。
自己也能制作毒鸡汤?那太好了,毒性够强,如果让别人扎到心你就厉害了。
每条毒汤可以点扎心、发毒评,或者转发给别人,让别人也扎扎心。
以上为毒汤日历 APP 介绍,官网 http://www.dutangapp.cn/ 当然官网什么都没有
可以浏览我给的这个,嘿嘿! 每天都有新的录入 https://svip.pp.ua/
准备工作
通过抓包得到了毒汤日历的 API http://www.dutangapp.cn/u/toxic?date=2018-6-18
API 后面的 data=xxxx-xx-xx
为日期
开始爬展
爬虫
- <?php
- header(“Content-type: text/html; charset=utf-8”); //设置编码 utf-8
- $utime = date(“Y-m-d”);//api的尾缀时间
- //使用curl提高运行速度 不用动
- function httpGet($url) {
- $curl = curl_init();
- $httpheader[] = “Accept:*/*”;
- $httpheader[] = “Accept-Language:zh-CN,zh;q=0.8”;
- $httpheader[] = “Connection:close”;
- curl_setopt($curl, CURLOPT_USERAGENT, “Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1” );
- curl_setopt($curl, CURLOPT_HTTPHEADER, $httpheader);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_TIMEOUT, 3);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($curl, CURLOPT_URL, $url);
- $res = curl_exec($curl);
- curl_close($curl);
- return $res;
- }
- $myfile = fopen(“binduyan.txt”, “a+”);//创建文件保存抓取的句子
- //循环次数 2018-3-21 至现在日期相差的天数
- for ($i=1; $i<83; $i++) {
- $json_string =httpGet(‘http://www.dutangapp.cn/u/toxic?date=’.$utime);//curl 自定义函数访问api
- $data= json_decode($json_string,true);//解析json 转为php
- //2018-4-11之前只有一条数据 so 加判断
- if (isset($data[‘data’][‘0’][‘data’])) {
- $text1= $data[‘data’][‘0’][‘data’].“\n”;
- fwrite($myfile, $text1);
- }
- if (isset($data[‘data’][‘1’][‘data’])) {
- $text2= $data[‘data’][‘1’][‘data’].“\n”;
- fwrite($myfile, $text2);
- }
- if (isset($data[‘data’][‘2’][‘data’])) {
- $text3= $data[‘data’][‘2’][‘data’].“\n”;
- fwrite($myfile, $text3);
- }
- $utime= date(“Y-m-d”,strtotime(“-“.strval($i).” day”)); //每循环一次 当前日期减去循环变量
- }
- fclose($myfile);
- echo “ok”;
- ?>
访问这个页面后 会在自动生成 binduyan.txt 文件,大概 200 多条。
搭建 API
- <?php
- header(“Content-type: text/html; charset=utf-8”); //设置编码 utf-8
- //获取句子文件的绝对路径
- //如果你介意别人可能会拖走这个文本,可以把文件名自定义一下,或者通过Nginx禁止拉取也行。
- $path = dirname(__FILE__);
- $file = file($path.“/binduyan.txt”);
- //随机读取一行
- $arr = mt_rand( 0, count( $file ) – 1 );
- $content = trim($file[$arr]);
- //编码判断,用于输出相应的响应头部编码
- if (isset($_GET[‘charset’]) && !emptyempty($_GET[‘charset’])) {
- $charset = $_GET[‘charset’];
- if (strcasecmp($charset,“gbk”) == 0 ) {
- $content = mb_convert_encoding($content,’gbk’, ‘utf-8’);
- }
- } else {
- $charset = ‘utf-8’;
- }
- //格式化判断,输出js或纯文本
- if ($_GET[‘encode’] === ‘js’) {
- echo “function binduyan(){document.write(‘” . $content .“‘);}”;
- } else {
- echo $content;
- }
- ?>
使用方法
将 API 代码保存为 index.php 与 binduyan.txt 上传到网站 binduyan 目录内
请求地址
http://你的域名/binduyan/
(返回随机一句)
http://你的域名/binduyan/index.php/?encode=js
(返回 js 格式)
博客集成
- <script type=“text/javascript” src=“http://你的域名/binduyan/index.php/?encode=js&charset=utf-8”></script>
- <div id=“binduyan”><script>binduyan()</script></div>
你也可以自己美化
你以为这就完了吗?
番外
区区 200 条怎么能够用呢
毒汤日历每日都会更新 那么为什么不自动每天抓取呢?
- <?php
- header(“Content-type: text/html; charset=utf-8”); //设置编码 utf-8
- $utime = date(“Y-m-d”);
- $str = file_get_contents(‘data.txt’);
- $d=date(‘Y/m/d H:i’,strtotime($str));
- //请更改监控key 默认binduyan
- if($_GET[‘p’]===’binduyan’){
- //判断今天是否已爬
- if(strtotime($utime)>strtotime($d)){
- //爬虫开始
- $utime = date(“Y-m-d”);//api的尾缀时间
- //使用curl提高运行速度 不用动
- function httpGet($url) {
- $curl = curl_init();
- $httpheader[] = “Accept:*/*”;
- $httpheader[] = “Accept-Language:zh-CN,zh;q=0.8”;
- $httpheader[] = “Connection:close”;
- curl_setopt($curl, CURLOPT_USERAGENT, “Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1” );
- curl_setopt($curl, CURLOPT_HTTPHEADER, $httpheader);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_TIMEOUT, 3);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($curl, CURLOPT_URL, $url);
- $res = curl_exec($curl);
- curl_close($curl);
- return $res;
- }
- //定义写入函数
- function myfile($txt){
- $myfile = fopen(“binduyan.txt”, “a+”);
- fwrite($myfile,$txt);
- fclose($myfile);
- }
- $json_string =httpGet(‘http://www.dutangapp.cn/u/toxic?date=’.$utime);//curl 自定义函数访问api
- $data= json_decode($json_string,true);//解析json 转为php
- //2018-4-11之前只有一条数据 so 加判断
- if (isset($data[‘data’][‘0’][‘data’])) {
- $text1=“\n”. $data[‘data’][‘0’][‘data’].“\n”;
- myfile($text1);
- }
- if (isset($data[‘data’][‘1’][‘data’])) {
- $text2= $data[‘data’][‘1’][‘data’].“\n”;
- myfile($text2);
- }
- if (isset($data[‘data’][‘2’][‘data’])) {
- $text3= $data[‘data’][‘2’][‘data’];
- myfile($text3);
- }
- $myfile = fopen(“data.txt”, “w”);
- fwrite($myfile,$utime);
- fclose($myfile);
- echo “ok”;
- //爬虫结束
- }else{
- echo “已爬”;
- }
- }else
- echo “老铁 搞事情吗”;
- ?>
转载自:Bin’BLog 文章链接:http://fooor.cn/index.php/archives/43/