CURL的模擬登錄和抓取頁面


<?php
$curl = curl_init();// 初始化
// 准備提交的表單數據之賬號和密碼。(這個是根據表單選項來的)
$data = "_username=604989282@qq.com&_password=6481485" ;
// 這個配置是為了防盜鏈,
curl_setopt($curl,CURLOPT_REFERER,"http://www.ydma.cn/");
// 基本配置
curl_setopt($curl, CURLOPT_URL, 'http://www.ydma.cn/login/check');// 網址
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);// 不輸出
curl_setopt($curl, CURLOPT_POST, 1);// POST方式
curl_setopt($curl, CURLOPT_POSTFIELDS,$data);// POST數據
curl_setopt($curl, CURLOPT_HTTPHEADER, array(// 頭部信息類型
  "content-type: application/x-www-form-urlencoded",
));

// Cookie相關設置
date_default_timezone_set("PRC");
curl_setopt($curl,CURLOPT_COOKIESESSION,TRUE);// 開啟cookie和session
curl_setopt($curl,CURLOPT_COOKIEFILE,"cookiefile");// 存儲名稱
curl_setopt($curl,CURLOPT_COOKIEJAR,"cookiefile");// 存儲名稱
curl_setopt($curl,CURLOPT_COOKIE,session_name()."=".session_id());// 存儲的是session_name()和session_id()

curl_setopt($curl,CURLOPT_FOLLOWLOCATION,1);// 設置可以跳轉
curl_setopt($curl,CURLOPT_HEADER,0);// 不去打印頭部信息

curl_setopt($curl,CURLOPT_REFERER,"http://www.ydma.cn/");



// 設置一下跳轉頁面
// curl_setopt($curl,CURLOPT_URL,"http://www.ydma.cn/user/52897/learn");
curl_setopt($curl,CURLOPT_URL,"http://www.ydma.cn/course/137/preview/lesson/3961");
  
curl_setopt($curl,CURLOPT_POST,0);

curl_setopt($curl,CURLOPT_HTTPHEADER,array("Content-type:text/html"));

curl_setopt($curl,CURLOPT_REFERER,"http://www.ydma.cn/");

$res = curl_exec($curl);

curl_close($curl);

// echo str_replace('src="/files', 'src="http://www.ydma.cn/files', $res);

// echo substr_replace($res,"<base href='http://www.ydma.cn/'>",strpos("<head>",$res),0);
echo str_replace("<head>","<head><base href='http://www.ydma.cn/'>",$res);
// echo $res;

頁面來源:https://www.jianshu.com/p/682ddef25f55

測試一個網站,因為需要驗證碼沒獲取到信息


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM