php - XPath 不检索某些内容
问题描述
我是一个新手,试图编写一个爬虫来从论坛中获取一些统计信息。
这是我的代码:
<?php
$ch = curl_init();
$timeout = 0; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'http://m.jeuxvideo.com/forums/42-51-61913988-1-0-1-0-je-code-un-bot-pour-le-forom-je-vous-le-montre-en-action.htm');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($file_contents);
$xpath = new DOMXPath($dom);
$posts = $xpath->query("//div[@class='who-post']/a");//$elements = $xpath->query("/html/body/div[@id='yourTagIdHere']");
$dates = $xpath->query("//div[@class='date-post']");//$elements = $xpath->query("/html/body/div[@id='yourTagIdHere']");
$contents = $xpath->query("//div[@class='message text-enrichi-fmobile text-crop-fmobile']/p");//$elements = $xpath->query("/html/body/div[@id='yourTagIdHere']");
$i = 0;
foreach ($posts as $post) {
$nodes = $post->childNodes;
foreach ($nodes as $node) {
$value = trim($node->nodeValue);
$tab[$i]['author'] = $value;
$i++;
}
}
$i = 0;
foreach ($dates as $date) {
$nodes = $date->childNodes;
foreach ($nodes as $node) {
$value = trim($node->nodeValue);
$tab[$i]['date'] = $value;
$i++;
}
}
$i = 0;
foreach ($contents as $content) {
$nodes = $content->childNodes;
foreach ($nodes as $node) {
$value = $node->nodeValue;
echo $value;
$tab[$i]['content'] = trim($value);
$i++;
}
}
?>
<h1>Participants</h2>
<pre>
<?php
print_r($tab);
?>
</pre>
如您所见,代码不会检索某些内容。例如,我试图从以下位置检索此内容:http: //m.jeuxvideo.com/forums/42-51-61913988-1-0-1-0-je-code-un-bot-pour-le-forom -je-vous-le-montre-en-action.htm
第二个帖子是一张图片,我的代码不起作用。
另一方面,我想我犯了一些错误,我发现我的代码很难看。
你能帮我吗 ?
解决方案
您可以简单地先选择帖子,然后使用以下方法分别获取每个子数据:
DOMXPath::evaluate
结合normalize-space
检索纯文本,DOMXPath::query
结合DOMDocument::save
检索消息段落。
代码:
$xpath = new DOMXPath($dom);
$postsElements = $xpath->query('//*[@class="post"]');
$posts = [];
foreach ($postsElements as $postElement) {
$author = $xpath->evaluate('normalize-space(.//*[@class="who-post"])', $postElement);
$date = $xpath->evaluate('normalize-space(.//*[@class="date-post"])', $postElement);
$message = '';
foreach ($xpath->query('.//*[contains(@class, "message")]/p', $postElement) as $messageParagraphElement) {
$message .= $dom->saveHTML($messageParagraphElement);
}
$posts[] = (object)compact('author', 'date', 'message');
}
print_r($posts);
无关说明:抓取网站的 HTML 本身并不违法,但您应避免在未经他们同意的情况下在您自己的应用程序/网站上显示他们的数据。此外,如果他们决定更改其 HTML 结构/CSS 类名,这可能会在任何时候中断。
推荐阅读
- python - 如何使用 pyspark 在 s3 上获取 csv(方案没有文件系统:s3n)
- python - 结合递归和收益进行树遍历
- intellij-idea - 为什么 compojure-app 和 hiccup 不能导入函数 hiccup.form/form-to?
- mongodb - MongoDbContext 用于 EF 核心中的数据迁移?
- javascript - 箭头函数在没有花括号的情况下运行完美。添加了花括号{ return },它会中断
- java - 切换 BottomNavigationView 选项卡会导致崩溃 E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
- onelogin - 如何在 Google Chrome 上禁用 OneLogin 扩展的 Command + U 功能?
- python - 检查一个元素是否等于一个大数组的所有数组元素的第一个元素
- ios - 如何告诉我的应用忽略特定触摸?
- python - 与 GPU 一起使用时,packed_padded_sequence 会出错