采集WordPress数据前需要准备两个基础环境:本地开发环境和目标网站访问环境。
安装PHP 7.4及以上版本,并启用以下扩展:
在Ubuntu系统上安装命令:
sudo apt update
sudo apt install php php-curl php-dom php-fileinfo php-mbstring php-openssl php-xml
使用Composer安装采集所需的核心库:
composer require guzzlehttp/guzzle
composer require symfony/dom-crawler
composer require symfony/css-selector
composer require league/html-to-markdown
使用浏览器开发者工具(F12)分析目标网站:
/page/{页码}/或/?paged={页码}article.post、div.post、content article创建headers.php配置文件:
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8',
'Accept-Language' => 'zh-CN,zh;q=0.9,en;q=0.8',
'Accept-Encoding' => 'gzip, deflate',
'Connection' => 'keep-alive',
'Upgrade-Insecure-Requests' => '1',
'Cache-Control' => 'max-age=0',
];
创建WordPressCrawler.php:
baseUrl = rtrim($baseUrl, '/');
$this->client = new Client([
'timeout' => 30,
'verify' => false,
'headers' => require 'headers.php'
]);
}
public function fetchPage($url) {
try {
$response = $this->client->get($url);
return (string)$response->getBody();
} catch (\Exception $e) {
echo "获取页面失败: {$url}\n错误: {$e->getMessage()}\n";
return null;
}
}
}
在WordPressCrawler类中添加方法:
public function fetchArticleLinks($listUrl, $maxPages = 10) {
$articles = [];
for ($page = 1; $page <= $maxPages; $page++) {
$url = str_replace('{page}', $page, $listUrl);
$html = $this->fetchPage($url);
if (!$html) break;
$crawler = new Crawler($html);
// 提取文章链接,根据实际网站调整选择器
$links = $crawler->filter('article a.entry-title-link, h2.entry-title a, h2.post-title a');
if ($links->count() === 0) break;
$links->each(function (Crawler $node) use (&$articles) {
$href = $node->attr('href');
if ($href && !in_array($href, $articles)) {
$articles[] = [
'url' => $href,
'title' => trim($node->text())
];
}
});
echo "已采集第 {$page} 页,找到 {$links->count()} 篇文章\n";
sleep(rand(2, 5)); // 避免请求过快
}
return $articles;
}
添加文章内容提取方法:

public function fetchArticleDetail($articleUrl) {
$html = $this->fetchPage($articleUrl);
if (!$html) return null;
$crawler = new Crawler($html);
$article = [];
// 提取标题
$title = $crawler->filter('h1.entry-title, h1.post-title, h1.page-title, header h1');
$article['title'] = $title->count() > 0 ? trim($title->text()) : '';
// 提取发布时间
$date = $crawler->filter('time.entry-date, .post-date, .published, meta[property="article:published_time"]');
if ($date->count() > 0) {
$article['publish_date'] = $date->attr('datetime') ?? trim($date->text());
}
// 提取正文内容
$content = $crawler->filter('.entry-content, .post-content, .article-content, content .content');
if ($content->count() > 0) {
$article['content'] = $content->html();
} else {
// 备用选择器
$content = $crawler->filter('article');
$article['content'] = $content->count() > 0 ? $content->html() : '';
}
// 提取分类标签
$categories = [];
$crawler->filter('.cat-links a, .post-categories a, .entry-categories a')->each(
function (Crawler $node) use (&$categories) {
$categories[] = trim($node->text());
}
);
$article['categories'] = $categories;
// 提取标签
$tags = [];
$crawler->filter('.tags-links a, .post-tags a, .entry-tags a')->each(
function (Crawler $node) use (&$tags) {
$tags[] = trim($node->text());
}
);
$article['tags'] = $tags;
$article['url'] = $articleUrl;
$article['crawled_at'] = date('Y-m-d H:i:s');
return $article;
}
创建DataProcessor.php处理采集到的数据:
]>(.?)<\/script>/is', '', $html);
// 移除style标签
$html = preg_replace('/