我正试图刮掉某个页面的表,但我有麻烦这样做。我想从表中获取列name, level和experience。这个表的每一行都有不同的背景颜色(斑马图案或其他什么名字),这使得它更困难。
我使用SimpleHTMLDom来做到这一点,这是我的代码:
<?php
include('simple_html_dom.php');
function scrapeHighscore($world) {
$html = str_get_html('https://secure.tibia.com/community/?subtopic=highscores&world=' . $world);
$tables = $html->find('table');
foreach($tables as $table) {
$names = $table->find('a');
foreach($names as $name) {
return $name . ' - ';
}
$levels = $table->find('td[@width="15%"]');
foreach ($levels as $level) {
return $level . ' - ';
}
$experiences = $table->find('td[@width="20%"]');
foreach ($experiences as $experience) {
return $experience . '<br>';
}
}
}
echo scrapeHighscore('Antica');
?>
我想要抓取的一个示例网站是:https://secure.tibia.com/community/?subtopic=hilicores&世界= Antica
您可以在该表中看到ID、Name、Level和Experience。我想只刮掉名字/级别/经验,然后像这样打印在我的网站上:
Meendel 536 2538560931
Drendaric 514 2237710308
Magicalse 509 2180132453
...
King Migoon 446 1460356304
我该怎么做呢?到目前为止,我的脚本没有返回任何东西。这只是一个空白页。
function scrapeHighscore($world) {
$output = '';
$html = str_get_html('https://secure.tibia.com/community /?subtopic=highscores&world=' . $world);
$tables = $html->find('table');
foreach($tables as $table) {
$names = $table->find('a');
foreach($names as $name) {
$output.= $name . ' - ';
}
$levels = $table->find('td[@width="15%"]');
foreach ($levels as $level) {
$output.= $level . ' - ';
}
$experiences = $table->find('td[@width="20%"]');
foreach ($experiences as $experience) {
$output.= $experience . '<br>;
}
}
return $output;
}
echo scrapeHighscore('Antica');
返回所有结果为只打印名字的内容。
如果你什么也没得到,就这样做:
$html = str_get_html('https://secure.tibia.com/community /?subtopic=highscores&world=' . $world);
echo $html;
看看你得到了什么。如果没有回音,你应该检查str_get_html
是如何工作的,问题可能在哪里。