杨小杰博客提供免费教程下载和网站搭建技术教程,主要分享和发布网站源码,致力创造一个高质量网络资源教程的分享平台

采集金山词霸每日一句

Young小杰2018-11-8 13:55网站搭建(0)250查询中……小标签: api 原创 源码

前几天一个网友我一个金山词霸的每日一句api,叫我写一下采集源码,其实难度不大,直接拿《采集毒鸡汤》的源码稍微改改就能用。

Api官方接口

每日:http://open.iciba.com/dsapi/

查指定时间:http://sentence.iciba.com/index.php?c=dailysentence&m=getdetail&title=2018-11-06&_=1541655200812

搭建采集接口

<?php 
header("Content-type: text/html; charset=utf-8"); //设置编码 utf-8 
$t1 = microtime(true);
$utime = date("Y-m-d");//api的尾缀时间
$translation = '0';//翻译语句,0不采集,1采集
$content = '1';//英语版,0不采集,1采集
//使用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("iciba.txt", "a+");//创建文件保存抓取的句子
//循环次数 2018-7-31 至现在日期相差的天数 
for ($i=1; $i<100; $i++) { 
    $json_string =httpGet('http://sentence.iciba.com/index.php?c=dailysentence&m=getdetail&title='.$utime.'&_='.time());//curl 自定义函数访问api

    $data= json_decode($json_string,true);//解析json 转为php
    if (isset($data['note'])) {
    	$text1= $data['note']."\n"; 
    	fwrite($myfile, $text1);
    }

    if (isset($data['translation'])&&$translation==1) {
    	$text2= str_replace('小编的话:', '', $data['translation'])."\n";
    	fwrite($myfile, $text2);
    }
    if (isset($data['content'])&&$content==1) {
    	$text3= $data['content']."\n"; 
    	fwrite($myfile, $text3);
    }
    $utime= date("Y-m-d",strtotime("-".strval($i)." day")); //每循环一次 当前日期减去循环变量
} 
fclose($myfile);
$t2 = microtime(true);
echo 'ok,耗时'.round($t2-$t1,3).'秒';

?>
运行这个接口能采集到之前100天的。

一言接口

<?php
//获取句子文件的绝对路径
//如果你介意别人可能会拖走这个文本,可以把文件名自定义一下,或者通过Nginx禁止拉取也行。
$path = dirname(__FILE__);
$file = file($path."/iciba.txt");
 
//随机读取一行
$arr  = mt_rand( 0, count( $file ) - 1 );
$content  = trim($file[$arr]);
 
//编码判断,用于输出相应的响应头部编码
if (isset($_GET['charset']) && !empty($_GET['charset'])) {
    $charset = $_GET['charset'];
    if (strcasecmp($charset,"gbk") == 0 ) {
        $content = mb_convert_encoding($content,'gbk', 'utf-8');
    }
} else {
    $charset = 'utf-8';
}
 
//格式化判断,输出js或纯文本
if (isset($_GET['encode'])&&$_GET['encode'] === 'js') {
	header('Content-type: text/javascript;charset=utf-8'); 
    echo "function iciba(){document.write('" . $content ."');}";
} else {
    echo $content;
}

每日采集接口

<?php
header("Content-type: text/html; charset=utf-8"); //设置编码 utf-8 
$utime = date("Y-m-d");
$file_data = 'data.txt';
if(!file_exists($file_data)){
    fopen($file_data, "w");
}
$str = file_get_contents('data.txt');
$d=date('Y/m/d H:i',strtotime($str));

$translation = '0';//翻译语句,0不采集,1采集
$content = '1';//英语版,0不采集,1采集
//请更改监控key 默认iciba
if($_GET['p']==='iciba'){
//判断今天是否已爬
    if(strtotime($utime)>strtotime($d)){
//爬虫开始    
//使用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("iciba.txt", "a+");
    fwrite($myfile,$txt);
    fclose($myfile);
}

$json_string =httpGet('http://open.iciba.com/dsapi/');//curl 自定义函数访问api
$data= json_decode($json_string,true);//解析json 转为php
//2018-4-11之前只有一条数据 so 加判断 
if (isset($data['note'])) {
    $text1= $data['note']."\n"; 
    myfile($text1);
}

if (isset($data['translation'])&&$translation==1) {
    $text2= str_replace('小编的话:', '', $data['translation'])."\n";
    myfile($text2);
}
if (isset($data['content'])&&$content==1) {
    $text3= $data['content']."\n"; 
    myfile($text3);
}
$myfile = fopen("data.txt", "w");
fwrite($myfile,$utime);
fclose($myfile);
echo "ok";
//爬虫结束
}else{
    echo "已爬";
}
}else
echo "老铁 搞事情吗";
?>
之后可以把这个每日采集接口使用宝塔计划监控为每日采集即可。

ps:多的图文介绍也就不多说了,查看采集毒鸡汤的文章即可。



发表评论: