首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP模擬登錄搜狐廣告聯盟獲取數據:CURL自動化方案含驗證碼處理

     編輯:PHP 綜合知識

業務場景

每日自動抓取搜狐廣告聯盟昨日收益,避免人工登錄。難點:驗證碼無法自動識別。

解決策略

  1. 人工一次性輸入驗證碼登錄,獲取有效 Cookies
  2. 定時任務每 10 分鐘請求一次頁面保持 Cookies 活性
  3. 定時腳本攜帶 Cookies 直接請求數據頁面解析入庫

關鍵代碼片段

1. 獲取驗證碼圖片(建立 Cookies)

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://union.sogou.com/validateCode");
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); // 保存Cookies
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$img = curl_exec($ch);
header("Content-type: image/gif");
echo $img;

2. 提交登錄表單(含手動驗證碼)

$post = array(
    'loginFromPage' => 'homePage',
    'username' => 'xxxx',
    'password' => 'xxxx',
    'activecode' => $_POST['code'], // 人工輸入
    'button.x' => 14, 'button.y' => 16
);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); // 攜帶Cookies
$response = curl_exec($ch);

3. 解析數據頁面入庫

$dom = str_get_html($response);
$text = $dom->find('div.rtable table tbody tr', 1)->plaintext;
$data = preg_split("/\s+/", $text);
// 提取 money, shows, times 寫入數據庫

完整Demo下載

本站下載完整實例代碼

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved