Home > Article > Backend Development > Detailed explanation of examples of crawling web content in php
Detailed explanation of examples of crawling web content in php
Method one:
Use the file_get_contents
method to implement
$url = "http://news.sina.com.cn/c/nd/2016-10-23/doc-ifxwztru6951143.shtml"; $html = file_get_contents($url); //如果出现中文乱码使用下面代码 //$getcontent = iconv("gb2312", "utf-8",$html); echo "<textarea style='width:800px;height:600px;'>".$html."</textarea>";
The code is very simple and can be understood at a glance without explanation.
Method 2:
Use curl to implement
$url = "http://news.sina.com.cn/c/nd/2016-10-23/doc-ifxwztru6951143.shtml"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); $html = curl_exec($ch); curl_close($ch); echo "<textarea style='width:800px;height:600px;'>".$html."</textarea>";
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
Add this code to indicate that if the request is redirected, you can access the final request page, otherwise the request result will display the following content:
Related learning recommendations: php programming (video)
The above is the detailed content of Detailed explanation of examples of crawling web content in php. For more information, please follow other related articles on the PHP Chinese website!