Home > Article > Backend Development > php crawl webpage_PHP tutorial
Using php to capture the content of the page is very useful in actual development. For example, it can be used as a simple content collector to extract part of the content of the web page, etc. The captured content can be obtained by filtering it through regular expressions. To find the content you want, the following are several commonly used methods to use php to crawl the content of web pages.
1.file_get_contents
PHP code
$url = "http://www.phpzixue.cn"; $contents = file_get_contents($url); //如果出现中文乱码使用下面代码 //$getcontent = iconv("gb2312", "utf-8",$contents); echo $contents; ?> |
$url = "http://www.phpzixue.cn"; $ch = curl_init(); $timeout = 5; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); //在需要用户检测的网页里需要增加下面两行 //curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); //curl_setopt($ch, CURLOPT_USERPWD, US_NAME.":".US_PWD); $contents = curl_exec($ch); curl_close($ch); echo $contents; ?> |
$handle = fopen ("http://www.phpzixue.cn", "rb"); $contents = ""; do { $data = fread($handle, 1024); if (strlen($data) == 0) { break; } $contents .= $data; } while(true); fclose ($handle); echo $contents; ?> |
$url = "http://www.phpzixue.cn"; $ch = curl_init(); $timeout = 5; curl_setopt($ch, CURLOPT_URL, $url); |
$handle = fopen ("http://www.phpzixue.cn", "rb");
$contents = "";
do {
$data = fread($handle, 1024);
if (strlen($data) == 0) {
break;
}
$contents .= $data;
} while(true);
fclose ($handle);
echo $contents;
?> |