如何用php 编写网络爬虫
展开全部
php不太适合用来写网络爬虫,因为几乎没有现成的框架,或者成熟的下载机制,也不太适合做并发处理.
下载页面的话除了一个curl,就是file_get_contents,或者curl_multi来做并发请求.curl可以代理端口,虚假ip,带cookie,带header请求目标页面,下载完成之后解析页隐桥源面可以用queryList来解析html.写法类似jQuery.
提供给你我之前写的类:curl.php 希望可以帮到你.
QueryList.php和phpQuery.php由于文件太大了,没办法贴上来
<?php
class Http {
public function curlRequest($url, $postData = '', $timeOut = 10, $httpHeader = array()) {
$handle = curl_init ();
灶态 curl_setopt ( $handle, CURLOPT_URL, $url );
if ($httpHeader) {
curl_setopt($handle, CURLOPT_HTTPHEADER, $httpHeader);
}
curl_setopt ( $handle, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $handle, CURLOPT_HEADER, 0 ); curl_setopt ( $handle, CURLOPT_TIMEOUT, $timeOut );
curl_setopt ( $handle, CURLOPT_FOLLOWLOCATION, 消纯1 );
curl_setopt ( $handle, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt ( $handle, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt ( $handle, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36'); curl_setopt ( $handle, CURLOPT_ENCODING, 'gzip,deflate,sdch');
if (! empty ( $postData )) {
curl_setopt ( $handle, CURLOPT_POST, 1 );
curl_setopt ( $handle, CURLOPT_POSTFIELDS, $postData);
}
$result['response'] = curl_exec ( $handle );
$result['httpStatus'] = curl_getinfo ( $handle, CURLINFO_HTTP_CODE );
$result['fullInfo'] = curl_getinfo ( $handle );
$result['errorMsg'] = '';
$result['errorNo'] = 0;
if (curl_errno($handle)) {
$result['errorMsg'] = curl_error($handle);
$result['errorNo'] = curl_errno($handle);
}
curl_close ( $handle );
return $result;
}
}
?>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询