1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
| <!DOCTYPE html> <html>
<head> <style> </style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="style.css" /> <link rel="icon" type="image/x-icon" href="images/icon.ico" /> <title>高考分数线查询</title> </head>
<body>
<?php
$server_name = ""; $port = 3307; $username = ""; $password = ""; $database_name = "score_spider";
function create_database_if_not_exist($conn, $name) { if(!$conn->select_db($name)) { $sql = "CREATE DATABASE " . $name; if($conn->query($sql) === true) { echo "<p>Succeed creating database $name .</p>"; } else { echo "<p>Failed creating database: " . $conn->error . "<p>"; } } }
function is_table_exist($conn, $dbname, $tablename) { $conn->select_db($dbname); $sql = "SHOW TABLES LIKE '$tablename'"; $result = $conn->query($sql); return mysqli_num_rows($result) != 0; }
function create_table_if_not_exist($conn, $dbname, $tablename) { if(!is_table_exist($conn, $dbname, $tablename)) { $sql = "CREATE TABLE $tablename ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, 年份 INT NOT NULL, 学校 VARCHAR(255) NOT NULL, 文理分科 CHAR(31) NOT NULL, 录取批次 CHAR(31) NOT NULL, 录取分数线 SMALLINT NOT NULL )";
if($conn->query($sql) == true) { echo "<p>Succeed creating table " . $tablename . " in database " . $dbname . ".</p>"; } else { echo "<p>Failed creating database: " . $conn->error . ".</p>"; } } }
function create_all_tables_if_not_exist($conn, $db_name) { create_table_if_not_exist($conn, $db_name, "河北"); create_table_if_not_exist($conn, $db_name, "山西"); create_table_if_not_exist($conn, $db_name, "辽宁"); create_table_if_not_exist($conn, $db_name, "吉林"); create_table_if_not_exist($conn, $db_name, "黑龙江"); create_table_if_not_exist($conn, $db_name, "江苏"); create_table_if_not_exist($conn, $db_name, "浙江"); create_table_if_not_exist($conn, $db_name, "安徽"); create_table_if_not_exist($conn, $db_name, "福建"); create_table_if_not_exist($conn, $db_name, "江西"); create_table_if_not_exist($conn, $db_name, "山东"); create_table_if_not_exist($conn, $db_name, "河南"); create_table_if_not_exist($conn, $db_name, "湖北"); create_table_if_not_exist($conn, $db_name, "湖南"); create_table_if_not_exist($conn, $db_name, "广东"); create_table_if_not_exist($conn, $db_name, "海南"); create_table_if_not_exist($conn, $db_name, "四川"); create_table_if_not_exist($conn, $db_name, "贵州"); create_table_if_not_exist($conn, $db_name, "云南"); create_table_if_not_exist($conn, $db_name, "陕西"); create_table_if_not_exist($conn, $db_name, "甘肃"); create_table_if_not_exist($conn, $db_name, "青海"); create_table_if_not_exist($conn, $db_name, "台湾"); create_table_if_not_exist($conn, $db_name, "内蒙古"); create_table_if_not_exist($conn, $db_name, "广西"); create_table_if_not_exist($conn, $db_name, "西藏"); create_table_if_not_exist($conn, $db_name, "宁夏"); create_table_if_not_exist($conn, $db_name, "新疆"); create_table_if_not_exist($conn, $db_name, "北京"); create_table_if_not_exist($conn, $db_name, "上海"); create_table_if_not_exist($conn, $db_name, "天津"); create_table_if_not_exist($conn, $db_name, "重庆"); create_table_if_not_exist($conn, $db_name, "香港"); create_table_if_not_exist($conn, $db_name, "澳门"); }
?>
<?php
function transform_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; }
function test_input($province, $year, $school) { if(!preg_match("/^[0-9]*$/", $year)) return false; if(empty($province)) return false; if(empty($school)) return false; return true; }
?>
<form class="box" action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> method="get"> <h1> 分数查询 </h1> <input type="text" name="province" placeholder="省份" /> <input type="text" name="year" placeholder="年份" /> <input type="text" name="school" placeholder="学校" /> <input type="submit" name="" value="查询" /> </form>
<div class="scrtxt">
<p>数据管理与分析课程作业报告:<a href="https://aytony.top/?p=588" target="_blank">利用爬虫爬取历年学校的高考分数线 – MindStation</a> </p>
<?php
if($_SERVER["REQUEST_METHOD"] == "GET") {
$province = transform_input($_GET["province"]); $year = transform_input($_GET["year"]); $school = transform_input($_GET["school"]);
if(test_input($province, $year, $school)) {
$conn = new mysqli($server_name, $username, $password, "", $port); if($conn->connect_error) { die("Error connection: " . $conn->connect_error); }
$conn->query("set names 'utf8'"); $conn->query("set character_set_client=utf8"); $conn->query("set character_set_results=utf8");
if(is_table_exist($conn, $database_name, $province)) { $conn->select_db($database_name); if(!empty($year)) $sql = "SELECT * FROM " . $province . " WHERE 年份=" . $year . " AND 学校 LIKE '%" . $school . "%' ORDER BY 录取分数线 DESC"; else $sql = "SELECT * FROM " . $province . " WHERE 学校 LIKE '%" . $school . "%' ORDER BY 年份, 录取分数线 DESC"; $result = $conn->query($sql);
if (mysqli_num_rows($result) > 0) { echo "<table>"; echo "<tr><th>年份</th><th>学校</th><th>文理分科</th><th>录取批次</th><th>录取分数线</th></tr>"; while($row = mysqli_fetch_assoc($result)) { echo "<tr><th>" . $row["年份"]. "</th><th>" . $row["学校"]. "</th><th>" . $row["文理分科"]. "</th><th>" . $row["录取批次"] . "</th><th>" . $row["录取分数线"] . "</th></tr>"; } echo "</table>"; } else { echo "无结果"; } } else { echo "省份 " . $province . " 不存在"; }
$conn->close(); } else { echo "请输入正确的省份(必填)、年份(选填)和学校(必填)"; } }
?> </div>
</body>
</html>
|