日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

PHP爬取历史天气

發布時間:2023/12/18 php 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PHP爬取历史天气 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

PHP爬取歷史天氣

PHP作為宇宙第一語言,爬蟲也是非常方便,這里爬取的是從天氣網獲得中國城市歷史天氣統計結果。

程序架構

main.php

<?phpinclude_once("./parser.php");include_once("./storer.php");#解析器和存儲器見下文$parser = new parser();$storer = new storer();#獲得url列表$urlList = $parser->getCityList("http://lishi.tianqi.com/");#依次解析新的URL網站內容,并存到數據庫中foreach($urlList as $url){$data = $parser->getData($url);$storer->store($data);}

解析器

解析器提供兩個接口,一個是解析主頁,獲得url列表;另一個是解析每座城市的數據,獲得該城市的歷史天氣數據。

這里使用到的解析庫是phpquery,使用JQuery的查詢方式,簡單高效。

<?php#借助JQuery庫解析include_once("./phpQuery-onefile.php"); class parser {//獲取城市url列表function getCityList($url){//直接在線流下載phpQuery::newDocumentFile($url);//第一次選擇$links = pq(".bcity *");$urlList = [];foreach ($links as $link) {#第二次選擇$tmp = pq($link)->find('a')->attr('href');#過濾組標簽if ($tmp!="#" and $tmp!="") {#檢查urlif(strpos($tmp,"-")==false and filter_var($tmp, FILTER_VALIDATE_URL))$urlList[] = $tmp; #添加URL列表}}return $urlList;}//獲取某個城市的歷史氣候function getData($url){//直接在線流下載phpQuery::newDocumentFile($url);//第一次選擇$text = pq("div .tqtongji p")->text();#匹配城市$city = $this->match("/,(.+)共出現/",$text);#匹配天氣$rainy = $this->match("/雨(\d+)天/",$text);$cloudy = $this->match("/多云(\d+)天/",$text);$sunny = $this->match("/晴(\d+)天/",$text);$overcast = $this->match("/陰(\d+)天/",$text); #為了跟cloudy區分$snowy = $this->match("/雪(\d+)天/",$text);#匹配拼音$pinYin = $this->match("/http:\/\/lishi\.tianqi\.com\/(.*?)\/index\.html/",$url);$result["url"] = $url;$result["city"] = $city;$result["pinYin"] = $pinYin;$result["rainy"] = $rainy;$result["cloudy"] = $cloudy;$result["sunny"] = $sunny;$result["overcast"] = $overcast;$result["snowy"] = $snowy;return $result;}#正則解析function match($rule,$text){preg_match_all($rule, $text, $result);#有些地區不是所有天氣都有if(count($result[1])==0)return "0";return $result[1][0];} }

存儲器

使用MySQLi接口即可,代碼如下:

<?phpclass storer{public $mysqli;function __construct(){$this->mysqli = new mysqli('localhost', '***', '******', 'phpWeather');$this->mysqli->query("SET NAMES UTF8");}function store($data){$url = $data["url"];$city = $data["city"];$pinYin = $data["pinYin"];$rainy = $data["rainy"];$cloudy = $data["cloudy"];$sunny = $data["sunny"];$overcast = $data["overcast"];$snowy = $data["snowy"];#字符串在插入時要添加''來區分$insertData = "VALUES('$city','$pinYin',$rainy,$cloudy,$sunny,$overcast,$snowy,'$url');";#sql分開寫更加清楚$sql = "INSERT INTO record(city,pinYin,rainy,cloudy,sunny,overcast,snowy,url)".$insertData;$isok = $this->mysqli->query($sql);if($isok){echo "$city 數據添加成功\n";}else{echo $sql . "\n";echo "$city 數據添加失敗\n";}}function __destruct(){$this->mysqli->close();}} ?>

爬蟲結果

共爬取了3119座城市的從2011年到現在的歷史天氣,接下來的數據分析以及可視化留到下一篇博客講述。

轉載于:https://www.cnblogs.com/fanghao/p/7496469.html

總結

以上是生活随笔為你收集整理的PHP爬取历史天气的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。