采集PHPEMS题目并做成搜索引擎

数据库类:

sjhx.php

<?php

class Sj
{
    protected $conn;
    protected $res;

    function __construct()
    {
        // 1.链接数据库
        //if ($_SERVER['HTTP_HOST'] == 'poster.hdacgn.com') $passwd = '1935201a7515b830'; else $passwd = 'root';
        $conn = @new mysqli('localhost', 'sjhx', 'kNNf5EWPNz5SeGBD', 'sjhx');
        if ($conn->connect_error) {//返回链接错误号
            // 返回链接错误信息
            die('数据库链接错误 (' . $conn->connect_errno . ') '
                . $conn->connect_error);
        }
        // 2. 选择数据库
        $conn->select_db("sjhx") or die("选择数据库失败:" . $conn->error);
        $conn->set_charset('utf8');
        $conn->query("set character set 'utf8'");
        $conn->query("set names 'utf8'");
        $this->conn = $conn;
    }
    
    public function outSea($text, $select)
    {
        $text = addslashes($text);
        switch($select) {
            case '2':
                $sql = "SELECT content FROM all_qu WHERE content like '%$text%' AND type = '1'";
                break;
            case '3':
                $sql = "SELECT content FROM all_qu WHERE content like '%$text%' AND type = '2'";
                break;
            default :
                $sql = "SELECT content FROM all_qu WHERE content like '%$text%'";
        }
        $res = $this->conn->query($sql);
        if ($res) {
            $num = $res->num_rows;
            $array = array();
            for ($i = 0; $i < $num; $i ++) {
                 $row = $res->fetch_row();
                 $content = $row[0];
                 $array[] = $content;
            }
            return array(1, $num, $array);
        } else
            return array(0, 0, sqlError());
    }
    
    private function addSql($model, $type, $page, $content)
    {
        $content = addslashes($content);
        $model = addslashes($model);
        $page = addslashes($page);
        $type = addslashes($type);
        $sql = "INSERT INTO all_qu(`model`, `type`, `content`, `page`)VALUES('$model', '$type', '$content', '$page')";
        $res = $this->conn->query($sql);
        if ($res) {
            echo '(' .$model . '|' . $type . '|' . $page . ')<br>';
        } else
            echo $this->sqlError();
    }
    
    public function addContent($model = 8, $type = 1, $start = 1, $end = 625)
    {
        for ($page = $start; $page < $end + 1; $page ++) {
            $content = $this->printContent($model, $type, $page);
            $this->addSql($model, $type, $page, $content);
        }
    }
    
    public function printContent($qid = 8, $qtp = 1, $page = 1)
    {
//ini_set('memory_limit', '256M');

$cookies = 'PHPSESSID=39bace96b2729bd214c9013e3e584a28; exam_psid=39bace96b2729bd214c9013e3e584a28; exam_currentuser=%25D9%25A8q%259E%25F4%25DF%25AD%259DckT%25A3%2595%25A5%25EB%25D7%25A8%25D2%25EE%25DF%25D8%25DE%2599%2595Tk%25A3l%25AB%25A8%255B%2598%25A9%25A3%2595%25A7%25A3kcejT%25EB%25D3%25AC%25D7%25E2%25DB%25E1%25DC%2591%25A4%25A5%25A7%259F%25A4%25DC%2590t%25D7%25B3%259F%25A5%25A6Redhd%2598%25AC%25A7l%259D%25A9%259C%25A3%25A0%2595bia%2560i%25B0%25D0o%25C8%25DC%25D1%25D5%25A5%2591%2592ib%2594T%25B3%25E1s%259D%25B3%258E%25E6%25D1%25A3%25A4%259B%259F%259E%259B%25E8%2590t%25D7%25B3%259D%25A7%25A6Rbdb%255Ed%25AA%25A5g%2595%25AF%259C%25A1%25A0bSm%25A3jc%25AC%25A8%255B%25D7%25DE%25DF%25E6%25D5%259F%259F%2599%25A2%259F%25A7%25E8%25D7%259D%2586%25B4%25DF%25AD%259DjSjRk%25A5%25B2%259Fo%259E%259B%25DF%25D8%25DF%25A3%259A%25A1%259E%259C%25A1%25DF%25D7%25A7%25D8%25E2%25D9%25D8%258Ek%259Alaef%25A9%25A3r%2594%25AB%25A5%25A8%25A7%25A3kcejT%25EB%25D3%25AC%25D7%25E2%25DB%25E1%25E1%25A3%2596%25A4%259E%2591%259F%25DD%2590t%25D7%25B3%259D%25A5%25A6R%25A2%25A3bee%25A9%25A3k%259C%25AA%259D%25A6%258Ek%25A4lafl%259A%25E1%259E%25D7%25EC%25D5%25E2%25DA%25A4%259A%259F%2595%259C%259B%25E5%25D7%25AD%2586%25B4%25D5%25AD%259Deeceib%25AA%25A7n%259F%25EC%25A6%25AC%25A6R%25A4%2597%25A3%25A3%259B%25E7%25DC%25A2%25C8%259B%25A7%25E6%25A6cclRck%25DA%25CF%259C%25C9%25B2%25A2%25D5%259Egck%2592%2594d%25A9%25A2%259C%259D%25A9%259D%25A6%25D1c%2596ghd%2593%25AA%25A6%255B%259F%25F6; __guid=95372188.197431853287025020.1541590324616.3796; exam_questype=' . $qtp . '; exam_knowsid=' . $qid . '; monitor_count=3; exam_number=1';
$url = 'http://122.227.160.206:6777/index.php?exam-app-lesson-ajax-questions&number=' . $page;
//https://danbooru.donmai.us/posts?ms=1&page=2&tags=hatsune_miku+rating%3Asafe+&utf8=%E2%9C%93
//$json = @file_get_contents($url);

//$get_array = json_decode($json, true);


//print_r($get_array);
//for ($i = 1, $i< 50, $i ++) {
$ch = curl_init($url);//初始化会话
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_COOKIE, $cookies);  //设置请求COOKIE
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  //将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
    $result = curl_exec($ch);
    //echo htmlentities($result);  //抓取的结果

//echo $result;  //抓取的结果


$search = array ("'<script[^>]*?>.*?</script>'si", // 去掉 javascript 
"'<style[^>]*?>.*?</style>'si", // 去掉 css 
"'<[/!]*?[^<>]*?>'si", // 去掉 HTML 标记 
"'<!--[/!]*?[^<>]*?>'si", // 去掉 注释标记 
"'([rn])[s]+'", // 去掉空白字符 
"'&(quot|#34);'i", // 替换 HTML 实体 
"'&(amp|#38);'i", 
"'&(lt|#60);'i", 
"'&(gt|#62);'i", 
"'&(nbsp|#160);'i", 
"'&(iexcl|#161);'i", 
"'&(cent|#162);'i", 
"'&(pound|#163);'i", 
"'&(copy|#169);'i", 
"# #"); // 作为 PHP 代码运行 

$replace = array ("", 
"", 
"", 
"", 
"\1", 
"\"", 
"&", 
"<", 
">", 
"", 
chr(161), 
chr(162), 
chr(163), 
chr(169), 
""); 
//$document为需要处理字符串,如果来源为文件可以$document = file_get_contents('http://www.sina.com.cn'); 
$out = preg_replace($search, $replace, $result); 
return $out; 

    }

    private function sqlError()
    {
        return $this->conn->error;
    }

    function __destruct()
    {
        // TODO: Implement __destruct() method.
        // 10.释放结果集资源
        if (isset($this->res) && !is_bool($this->res))
            $this->res->close();// $res->free();
        // 关闭数据库连接
        $this->conn->close();
    }
}

采集页面

sqlin.php

<?php
$t1 = microtime(true);
require_once './sjhx.php';
$sql = new Sj();
$model = '';
$type = '';
$start = '';
$end = '';
if (isset($_GET['model']))
    $model = $_GET['model'];
if (isset($_GET['type']))
    $type = $_GET['type'];
if (isset($_GET['start']))
    $start = $_GET['start'];
if (isset($_GET['end']))
    $end = $_GET['end'];

?>
<header>
<meta name="viewport" content="initial-scale=1.0,width=device-width,user-scalable=0,maximum-scale=1.0"/>
</header>
<form method="get" action="">
            题型,数字ID<input name="model" type="text" value="<?=$model?>"/> <br> 类型,数字ID<input name="type" type="text" value="<?=$type?>"/> <br>  开始题号<input name="start" type="text" value="<?=$start?>"/>  <br> 结束题数号<input name="end" type="text" value="<?=$end?>"/> <br> <input type="submit" value="开始录入"/>
            <p></p>
        </form>
        
<?php

if ($model != '' && $type != '' && $start != '' && $end != '')
    $sql->addContent($model, $type, $start, $end);

$t2 = microtime(true);
echo '耗时'.round($t2-$t1,3).'秒<br>';
echo 'Now memory_get_usage: ' . memory_get_usage() / 1024 . '(KB)<br />';

搜索页面

search.php/html

<!DOCTYPE html>
    <html lang="zh-CN">
<header>
        <meta name="viewport" content="initial-scale=1.0,width=device-width,user-scalable=0,maximum-scale=1.0"/>
        <title></title>
        <script src="https://cdn.bootcss.com/jquery/1.9.1/jquery.min.js"></script>
        <link rel="stylesheet" href="//cdnjs.loli.net/ajax/libs/mdui/0.4.2/css/mdui.min.css">
        <script src="//cdnjs.loli.net/ajax/libs/mdui/0.4.2/js/mdui.min.js"></script>
        <script src="./function.js?r=<?=md5(uniqid(md5(microtime(true)),true))?>"></script>
</header>
<body>

<div class="mdui-container">
<div id="hide" style="magin-top: 20px;">
<h1 style="text-align:center;"></h1>
</div>
<button class="back-button mdui-shadow-0 mdui-fab mdui-fab-fixed mdui-ripple" onclick="$('#all').show();$('#hide').hide();$('.back-button').removeClass('mdui-shadow-0');$('.back-icon').html('arrow_back');"><i class="mdui-icon material-icons back-icon"></i></button>
<div id="all" style="display: none;">
    <div class="mdui-textfield mdui-textfield-floating-label">
        <label class="mdui-textfield-label">请输入关键字</label>
        <input class="mdui-textfield-input" id="search" type="text" value=""/>
    </div>
    <select id="sel" class="mdui-select" mdui-select>
        <option value="1">全部</option>
        <option value="2">选择</option>
        <option value="3">判断</option>
    </select>
    <br><br>
        <button class="search-button mdui-color-blue-a200 mdui-btn mdui-btn-raised mdui-ripple">模糊搜索</button>
        <button class="clear mdui-btn mdui-btn-raised mdui-ripple" onclick="$('#search').val('');$('#search').focus();">清空</button>
        <br><br>
        <button class="tip-button mdui-color-yellow-a400 mdui-btn mdui-btn-raised mdui-ripple">提示</button>
        <button class="hide-button mdui-color-red-a400 mdui-btn mdui-btn-raised mdui-ripple" type="button" onclick="$('#all').hide();$('#hide').show();$('.back-button').addClass('mdui-shadow-0');$('.back-icon').html('');">掩藏</button>
<hr>
<div id="list"></div>
  <div class="mdui-dialog" id="dialog">
    <div class="mdui-dialog-title">提示</div>
    <div class="mdui-dialog-content">
      <div class="mdui-typo">
        <ul>
    <li>输入关键字点回车或者按模糊搜索按钮进行查询,关键字不要太少,而且关键字必须是连续的字。</li>
    <li>全部选项包括选择和判断。</li>
    <li>点击清空即可清空输入框文字。</li>
    <li>点击掩藏即可防止被发现,点击右下角隐藏起来的返回按键即可返回。</li>
    <li>Ctrl+TAB可迅速切换标签页,Ctrl+Shift+H快速掩藏。一般使用为复制,显示,清空,粘贴,回车/搜索,掩藏。</li>
    <li>记得清除历史记录。</li>
      </lu>
    </div>
    </div>
    <div class="mdui-dialog-actions">
      <button class="mdui-btn mdui-ripple" mdui-dialog-cancel>知道了</button>
    </div>
  </div>
</div>
</body>
</html>

Js:function.js

$(function(){
document.title='\u200E';
$(".search-button").on('click',function () {
    var sel = $("#sel").val();
    var inputText = $("#search").val();
    if (inputText === "") {
        mdui.snackbar({
            message: '请输入文字'
        });
        $("#search").focus();
        return 0;
    }
    $("#list").html('<div class="mdui-progress"><div class="mdui-progress-indeterminate"></div></div>');
    $.ajax({
        url: "action",
        type: "POST",
        async: true,
        timeOut: 50000,
        dataType: "json",
        data: {
            type: "search",
            text: inputText,
            select: sel
        },
        success: function (data, textStatus, jqXHR) {
            if (data.content == 1) {
                $("#list").html(data.list);
            } else if (data.content == 0) {
                alert(data.error);
            }
            // alert(data);
        },
        error: function (xhr, textStatus) {
            console.log(xhr + textStatus);
        }
    });
})


$(".tip-button").on('click',function () {
    var inst = new mdui.Dialog('#dialog');
    inst.open();
})

})

$("body").keydown(function() {
    if (window.event.keyCode == "13") {//keyCode=13是回车键;数字不同代表监听的按键不同
        search();
    }
    if (window.event.ctrlKey && window.event.shiftKey && window.event.keyCode == "72") {
        $(".hide-button").click();
    }
});

 

完成

预览

去除PHP后缀伪静态规则:

if (!-f $request_filename){
    rewrite "^/(.*)$" /$1.php;
}
点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注

16 − 7 =