實驗樓的php比賽題,網頁資料提取。

來源:互聯網
上載者:User

標籤:地址   amp   ase   www   oca   not   stat   https   ...   

實驗樓的php比賽題,網頁資料提取。

題目的地址:https://www.shiyanlou.com/contests/lou5/challenges

以下代碼是題目的答案

<?phpheader("Content-Type:text/html;charset=utf-8");class Crawler{ private $content; private $data; static private $mysql; public function __construct(){ echo "開始爬取內容...."; } public function loadFile($file_path){ echo "正在負載檔案"; $this->content = file_get_contents($file_path); } public function parseCourseBody(){ $regex = "/<body[^>]*?>(.*\s*?)<\/body>/is"; if(preg_match_all($regex, $this->content, $matches)){ $this->content = $matches[0]; } } public function parseContent(){ echo "開始解析內容...<br/>"; $this->parseCourseBody(); $this->parseTitle(); $this->parseDesc(); $this->parseType(); $this->titleIsLong(); $this->saveData(); echo "解析內容結束!<br/>"; } public function saveData(){ echo "存入資料庫...<br/>"; self::$mysql = mysql_connect("localhost","root","root"); mysql_query("set names utf8"); mysql_select_db("databases",self::$mysql); $cnames = $this->data[‘cnames‘]; $cdescs = $this->data[‘cdescs‘]; $ctypes = $this->data[‘ctypes‘]; $nlongs = $this->data[‘nlongs‘]; foreach ($cnames as $key => $value) { $sql = "insert into `course_data`(`cname`,`cdesc`,`ctype`,`nlong`) values(‘".$cnames[$key]."‘,‘".$cdescs[$key]."‘,‘".$ctypes[$key]."‘,‘".$nlongs[$key]."‘)"; mysql_query($sql); } mysql_close(); } public function parseTitle(){ echo "解析課程標題...<br/>"; $regex= "/<div class=\"course-name\".*?>.*?<\/div>/ism";   if(preg_match_all($regex, $this->content, $matches)){$cnames = $matches[0];}foreach ($cnames as &$value) {$value = str_replace("</div>","",str_replace("<div class=\"course-name\">", "", $value));}$this->data[‘cnames‘] = $cnames; } public function parseDesc(){ echo "解析課程簡介...<br/>"; $regex4= "/<div class=\"course-desc\".*?>.*?<\/div>/ism";   if(preg_match_all($regex, $this->content, $matches)){$cdescs = $matches[0];}foreach ($cdescs as &$value) {$value = str_replace("</div>","",str_replace("<div class=\"course-desc\">", "", $value));}$this->data[‘cdescs‘] = $cdescs; } public function parseType(){ echo "解析課程類型...<br/>"; $regex= "/<div class=\"course-footer\".*?>.*?<\/div>/ism";   if(preg_match_all($regex, $this->content, $matches)){$ctypes = $matches[0];}foreach ($ctypes as &$value) {$str = str_replace("</div>","",str_replace("<div class=\"course-footer\">", "", $value));if(preg_match_all("/([\x{4e00}-\x{9fa5}])/u", $str, $match)){$value = join("",$match[0]);}else{$value = "免費";}$this->data[‘ctypes‘] = $ctypes; } public function titleIsLong(){ echo "判斷課程名是否超長...<br/>"; $cnames = $this->data[‘cnames‘]; foreach ($cnames as $value) { $nlongs[] = mb_strlen($value) > 16 : "true" : "false"; } $this->data[‘nlongs‘] = $nlongs; }}$Crawler = new Crawler();$Crawler->loadFile("test.html");$Crawler->parseContent();/** 表結構cname(varchar):完整的課程名cdesc(varchar):課程描述ctype(varchar):課程類型,值為 免費,會員,訓練營。nlong(enum(‘true‘,‘false‘)):課程名是否過長,課程名稱超過16字元的時候為 true,否則為 falsecreate table `course_data`(`id` int(11) not null auto_increment,`cname` varchar(255) default null,`cdesc` varchar(255) default null,`ctype` varchar(255) default null,`nlong` enum(‘true‘,‘false‘) default null,primary key (`id`))engine=InnoDB default charset=utf8;*/

  

實驗樓的php比賽題,網頁資料提取。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.