Examples of crawling web content in php to capture Web Content
Examples of crawling web content in php
Method 1:
Use the file_get_contents Method
$ Url = "http://news.sina.com.cn/c/nd/2016-10-23/doc-ifxwztru6951143.shtml"; $ html = file_get_contents ($ url); // use the following code if Chinese characters are garbled // $ getcontent = iconv ("gb2312", "UTF-8 ", $ html); echo "<textarea style = 'width: 800px; height: 600px; '> ". $ html. "</textarea> ";
The code is very simple. You can understand it at first glance and don't explain it.
Method 2:
Use curl
$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);
With this code, the final request page can be accessed when the request is redirected. Otherwise, the request result will display the following content:
If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article and hope to help you. Thank you for your support for this site!