永发信息网

如何用php 编写网络爬虫

答案:1  悬赏:10  手机版
解决时间 2021-03-20 02:33
  • 提问者网友:城市野鹿
  • 2021-03-19 20:15
如何用php 编写网络爬虫
最佳答案
  • 五星知识达人网友:三千妖杀
  • 2021-03-19 20:43
php不太适合用来写网络爬虫,因为几乎没有现成的框架,或者成熟的下载机制,也不太适合做并发处理.
下载页面的话除了一个curl,就是file_get_contents,或者curl_multi来做并发请求.curl可以代理端口,虚假ip,带cookie,带header请求目标页面,下载完成之后解析页面可以用queryList来解析html.写法类似jQuery.
提供给你我之前写的类:curl.php  希望可以帮到你.
QueryList.php和phpQuery.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;
    }
}
?>
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯