切换风格

默认Lavender Sky Flowers Wizard Snow Beige California City Dragon Black London Sunset glow Pink Cloud

5

主题

111

积分

1

精华

用户组 

易积分
6560
热心
0
好评
15
PHP 自动爬毒汤日历搭建一言 API[复制链接]
发表于 2018-6-19 12:16:19 | 显示全部楼层 |阅读模式
本帖最后由 十一 于 2018-6-19 12:17 编辑

什么是毒汤日历
毒汤日历是一本有毒的日历,每天用毒鸡汤来唤醒你。
你甚至不用打开日历,打开 App 的推送,每天会定时送上一杯毒鸡汤。
自己也能制作毒鸡汤?那太好了,毒性够强,如果让别人扎到心你就厉害了。
每条毒汤可以点扎心、发毒评,或者转发给别人,让别人也扎扎心。

准备工作通过抓包得到了毒汤日历的 API http://www.dutangapp.cn/u/toxic?date=2018-6-18
API 后面的data=xxxx-xx-xx 为日期
爬虫采集
  1. <?php
  2. header("Content-type: text/html; charset=utf-8"); //设置编码 utf-8
  3. $utime = date("Y-m-d");//api的尾缀时间
  4. //使用curl提高运行速度 不用动
  5. function httpGet($url) {
  6.     $curl = curl_init();
  7.     $httpheader[] = "Accept:*/*";
  8.     $httpheader[] = "Accept-Language:zh-CN,zh;q=0.8";
  9.     $httpheader[] = "Connection:close";
  10.     curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
  11.     curl_setopt($curl, CURLOPT_HTTPHEADER, $httpheader);
  12.     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  13.     curl_setopt($curl, CURLOPT_TIMEOUT, 3);
  14.     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  15.     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  16.     curl_setopt($curl, CURLOPT_URL, $url);
  17.     $res = curl_exec($curl);
  18.     curl_close($curl);
  19.     return $res;
  20. }
  21. $myfile = fopen("binduyao.txt", "a+");//创建文件保存抓取的句子
  22. //循环次数 2018-3-21 至现在日期相差的天数
  23. for ($i=1; $i<83; $i++) {
  24.     $json_string =httpGet('http://www.dutangapp.cn/u/toxic?date='.$utime);//curl 自定义函数访问api
  25.     $data= json_decode($json_string,true);//解析json 转为php
  26. //2018-4-11之前只有一条数据 so 加判断
  27. if (isset($data['data']['0']['data'])) {
  28.      $text1= $data['data']['0']['data']."\n";
  29.     fwrite($myfile, $text1);
  30. }
  31. if (isset($data['data']['1']['data'])) {
  32.     $text2= $data['data']['1']['data']."\n";
  33.    fwrite($myfile, $text2);
  34. }   
  35. if (isset($data['data']['2']['data'])) {
  36.     $text3= $data['data']['2']['data']."\n";
  37.     fwrite($myfile, $text3);
  38. }

  39.     $utime= date("Y-m-d",strtotime("-".strval($i)." day")); //每循环一次 当前日期减去循环变量

  40. }
  41. fclose($myfile);
  42. echo "ok";
  43. ?>
复制代码
访问这个页面后 会在自动生成 binduyan.txt 文件,大概 200 多条。
搭建 API
  1. //获取句子文件的绝对路径
  2. //如果你介意别人可能会拖走这个文本,可以把文件名自定义一下,或者通过Nginx禁止拉取也行。
  3. $path = dirname(__FILE__);
  4. $file = file($path."/binduyan.txt");

  5. //随机读取一行
  6. $arr  = mt_rand( 0, count( $file ) - 1 );
  7. $content  = trim($file[$arr]);

  8. //编码判断,用于输出相应的响应头部编码
  9. if (isset($_GET['charset']) && !empty($_GET['charset'])) {
  10.     $charset = $_GET['charset'];
  11.     if (strcasecmp($charset,"gbk") == 0 ) {
  12.         $content = mb_convert_encoding($content,'gbk', 'utf-8');
  13.     }
  14. } else {
  15.     $charset = 'utf-8';
  16. }

  17. //格式化判断,输出js或纯文本
  18. if ($_GET['encode'] === 'js') {
  19.     echo "function binduyan(){document.write('" . $content ."');}";
  20. } else {
  21.     echo $content;
  22. }
复制代码

使用方法将 API 代码保存为 index.php binduyan.txt 上传到网站 binduyan 目录内
请求地址
http://你的域名/binduyan/(返回随机一句)
http://你的域名/binduyan/index.php/?encode=js(返回 js 格式)
博客集成
  1. <script type="text/javascript" src="http://你的域名/binduyan/index.php/?encode=js&charset=utf-8"></script>
  2. <div id="binduyan"><script>binduyan()</script></div>
复制代码

自用 API毒言
APihttp://duyan.fooor.cn/word.php
[td]
参数
encode 默认返回文本格式; js 返回为 JS 格式
charset 默认 UTF-8; GBK 返回 GBk 格式

举个栗子http://duyan.fooor.cn/word.php/?encode=js&charset=utf-8
古诗
APihttp://duyan.fooor.cn/word.php
[td]
参数
typed (必选)
encode 默认返回文字; js 返回为 JS 格式
charset 默认 UTF-8 ; GBK 返回 GBk 格式
author 默认返回作者; 1 返回作者

举个栗子http://duyan.fooor.cn/word.php/?type=d&author=1&charset=utf-8
php 初学者 如有错误请见谅 到此结束
本文为FOOOR.CN,转载请注明来源。






评分

参与人数 1易积分 +10 收起 理由
易如意 + 10 新技能已get√

查看全部评分

易如意中文编程学习交流论坛有你更精彩~
回复

使用道具 举报

5

主题

111

积分

1

精华

用户组 

易积分
6560
热心
0
好评
15
发表于 2018-6-19 12:18:52 | 显示全部楼层
排版有点问题 凑乎看吧
易如意中文编程学习交流论坛有你更精彩~
回复

使用道具 举报

5

主题

111

积分

1

精华

用户组 

易积分
6560
热心
0
好评
15
发表于 2018-6-19 12:22:16 | 显示全部楼层
有没有大佬换友链的
易如意中文编程学习交流论坛有你更精彩~
回复

使用道具 举报

11

主题

479

积分

0

精华

用户组 

易积分
1528
热心
0
好评
0

活跃会员

发表于 2018-6-26 20:09:55 | 显示全部楼层
已搭建  但是貌似楼主版的index代码少了一行  
易如意中文编程学习交流论坛没你也精彩~
回复

使用道具 举报

5

主题

111

积分

1

精华

用户组 

易积分
6560
热心
0
好评
15
发表于 2018-6-30 10:06:47 | 显示全部楼层
lvsj575 发表于 2018-6-26 20:09
已搭建  但是貌似楼主版的index代码少了一行

我博客重新上了源码
易如意中文编程学习交流论坛有你更精彩~
回复

使用道具 举报

0

主题

28

积分

0

精华

用户组 

易积分
20
热心
0
好评
0
发表于 2019-5-31 20:08:42 | 显示全部楼层
可以可以        
易如意中文编程学习交流论坛有你更精彩~
回复

使用道具 举报

QQ|sitemap|免责声明|RGB颜色对照表|手机版|小黑屋| 易如意 - E4A中文编程学习交流论坛

GMT+8, 2024-4-25 08:47 , Processed in 0.043405 second(s), 39 queries .

Powered by Discuz! X3.4

© 2001-2018 eruyi.cn

返回顶部