PHP獲取cookie、Token、模擬登錄、抓取數據、解析生成json


本文介紹使用PHP獲取cookie,獲取Token、以及模擬登錄、然后抓取數據、最后解析生成json的的過程。

 

0. 設置Cookie路徑

set_time_limit(0);

//使用的cookie路徑,
if (isset($_SERVER['HTTP_APPNAME'])){
    $cookie = SAE_TMP_PATH."/cookie.txt";
}else {
    $cookie = dirname(__FILE__)."/cookie.txt";
}

 

1、打開頁面,獲取COOKIEJAR,以及 token,並保存

$url = "http://www.fangbei.org/#agent/login";
$headers = array(
"User-Agent: 來源 方倍工作室 www.fangbei.org",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Accept-Language: zh-CN,zh;q=0.9,en;q=0.8",
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie); //首次只接收
$result = curl_exec($curl);
curl_close($curl);

$pattern = '/name="_token" value="(.*?)"/is';
preg_match_all($pattern, $result, $matches);
if (isset($matches[1][0])){
    $token = $matches[1][0];
}else{
    die("獲取token失敗");
}

 

2、登錄

 #2. 登錄
$url = "http://www.fangbei.org/#/agent/login";
$headers = array(
"User-Agent: 來源 方倍工作室 www.fangbei.org",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Accept-Language: zh-CN,zh;q=0.9,en;q=0.8",
"Origin: http://www.fangbei.org/#",
"Referer: http://www.fangbei.org/#/agent/login",
);

$fields = '_token='.$token.'&username=fangbei&password=fangbei.org';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);    //發送cookie
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);     //接收 cookie
curl_exec($curl);
curl_close($curl);

 

3. 取數據

$url = "http://www.fangbei.org/#agent/AgentProductLink";
$headers = array(
"User-Agent: 來源 方倍工作室 www.fangbei.org",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Accept-Language: zh-CN,zh;q=0.9,en;q=0.8",
"Referer: http://www.fangbei.org/#agent/welcome",
);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);    //發送cookie
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);     //接收 cookie
$content = curl_exec($curl);
curl_close($curl);

 

4、解析數據,生成json

require_once('simple_html_dom.php');
// var_dump($content);
$html_main = str_get_html($content);
if (!isset($html_main)){
    $html_main->clear();
    die("頁面載入出錯!");
}
$tjarray = array();
foreach($html_main->find('tr[class="cot"]') as $item)
{
    $id = @$item->find('td', 0)->plaintext;
    $title = @$item->find('td', 1)->plaintext;
    $button = @$item->find('div[class="btn btn-fill"]', 0)->outertext;
    $pattern = "/\(\'(.+?)\'\)/"; // copyUrl('http://www.fangbei.org/#top-apply/Affection-28679')
    preg_match_all($pattern, $button, $matches);
    $bturl = $matches[1][0];
    $tjarray[$id] = array("title"=>urlencode($title),"url"=>urlencode($bturl));
    // break;
}
$html_main->clear();
echo urldecode(json_encode($tjarray));

 


免責聲明!

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



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