本文示範如何使用 PHP 搭配 Snoopy 庫,從特定論壇頁面抓取迅雷 VIP 分享帳號。程式碼包含連結過濾、頁面抓取、編碼轉換與正則提取,適合學習網頁爬蟲基礎流程。
<?php
header('Content-Type: text/html; charset=UTF-8');
require_once 'Snoopy.php';
$listUrl = 'https://www.521xunlei.com/forum-xunleihuiyuan-1.html';
$snoopy = new Snoopy();
// 1. 取得列表頁所有連結
$snoopy->fetchlinks($listUrl);
$links = $snoopy->results;
// 2. 過濾:保留包含 'thread-' 且以 '-1-1.html' 結尾的主題連結
$filtered = array_filter($links, function($url) {
return strpos($url, 'thread-') !== false && strpos($url, '-1-1.html') !== false;
});
$filtered = array_values(array_unique($filtered));
// 3. 抓取第一篇主題頁面內容
$detail = new Snoopy();
$detail->fetchtext($filtered[1]);
$content = $detail->results;
$content = iconv('GBK', 'UTF-8//IGNORE', $content); // 原站為 GBK
// 4. 正則提取帳號資訊
$pattern = '/迅雷(會員|共享)賬號\s*[a-zA-Z0-9_]{4,15}\s*:\s*[0-9]+\s*愛密碼分享密碼\s*[a-zA-Z0-9_]{4,20}/';
preg_match_all($pattern, $content, $matches);
foreach ($matches[0] as $line) {
echo htmlspecialchars($line) . '<br>';
}
iconv('GBK', 'UTF-8//IGNORE', ...) 安全轉碼,忽略無法轉換的字元。⚠️ 重要:爬取並使用他人分享的付費帳號可能違反服務條款、侵犯智慧財產權,甚至觸犯電腦犯罪法相關條文。本範例僅供技術學習參考,請勿用於商業或非法用途。生產環境爬蟲請遵守 robots.txt、控制請求頻率、處理反爬機制,並尊重資料擁有者權益。