php爬虫

首先需要手动登陆获取cookie,代码登陆有验证码

public function actionIndex()
    {
        $cookie ="XXX";

        header("Content-type:text/html;charset=utf-8");

        //$data=‘XXX';
        $curlobj = curl_init();         // 初始化

        curl_setopt($curlobj,CURLOPT_HTTPHEADER , array(
        
        "Accept-Encoding: gzip, deflate, br",
        "Accept-Language: zh-CN,zh;q=0.9",
        "Cache-Control: max-age=0",
        "Connection: keep-alive",
        "Cookie: $cookie",

        "Host: www.douban.com",
        "Referer: https://www.douban.com/",
        "Upgrade-Insecure-Requests: 1",
        "User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36",

    ));
        curl_setopt($curlobj, CURLOPT_ENCODING, 'gzip');
        curl_setopt($curlobj, CURLOPT_URL, "https://www.douban.com/");      // 设置访问网页的URL
        curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, true);            // 执行之后不直接打印出来





        $output = curl_exec($curlobj);    // 执行
        curl_close($ch);// 关闭cURL
        echo $output;











    }

你可能感兴趣的:(php)