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));

 

posted @ 2019-08-16 12:11 方倍工作室 阅读(...) 评论(...) 编辑 收藏