HTML 简单 dom PHP 爬虫问题
Issue with HTML Simple dom PHP scraper
我正在尝试从这个 website 解析团队统计数据。
我要解析"Key stats"块,这里是截图
Wins / draws / losses - 363 / 8 / 168
Total kills - 50715
Total deaths - 45101
Rounds played - 14083
K/D Ratio - 1.12
Best player(Average rating) - olofmeister (1.15)
Screenshot(我没有代表,要加图,不好意思)
我正在使用 Simple HTML Dom PHP 解析器,我从最基本的开始。为了测试目的,我提取了所有链接,它对我来说效果很好。
include 'simple_html_dom.php';
$url = 'http://www.hltv.org/?pageid=179&teamid=4991&gameid=2';
$html = file_get_html($url);
foreach($html->find('a') as $element) {
echo $element->href . '<br>';
}
$html->clear();
unset($html);
之后,我开始提取主要的 div 块,其中保存了所有内容:
include 'simple_html_dom.php';
$url = 'http://www.hltv.org/?pageid=179&teamid=4991&gameid=2';
$html = file_get_html($url);
foreach ($html->find('div[style="float:right;width:300px;"]') as $div) {
echo $div . '<br/>';
};
它运行良好,结果令人满意 - prntscr。com/88p8l1
然后,我开始越陷越深,陷入困境。
include 'simple_html_dom.php';
$url = 'http://www.hltv.org/?pageid=179&teamid=4991&gameid=2';
$html = file_get_html($url);
foreach ($html->find('div[style="float:right;width:300px;"]') as $div) {
$item['stat-title'] = $html->find('div[style="height:22px;background-color:white"]')->plaintext;
$item['stat-data'] = $html->find('div[style="height:22px;background-color:white"]')->plaintext;
$items[] = $item;
};
print_r($items);
至此,我真的很纠结,如何显示我需要的结果。
我单独测试了代码的一部分 - 它工作正常。
foreach ($html->find('div[style="height:22px;background-color:#E6E5E5"]') as $div) {
echo $div . '<br/>';
};
我想要达到的结果:
<div class="stat">
<span class="stat-title">Wins / draws / losses</span>
<span class="stat-data">363 / 8 / 168</span>
</div>
我需要对我当前的问题有新的看法。提前谢谢你。
$item;
foreach ($html->find('div.covGroupBoxContent div.covSmallHeadline') as $div) {
if(isset($div->style) && $div->style=="font-weight:normal;width:180px;float:left;color:black;text-align:right;") {
//select black text which is the stat data
$item["stat-data"] = $div->plaintext;
//the previous sibling of the data is the title (based on the website)
$item["stat-title"] = $div->prev_sibling()->plaintext;
$items[] = item;
}
};
希望这对您有所帮助。请正确定义问题。
我正在尝试从这个 website 解析团队统计数据。
我要解析"Key stats"块,这里是截图
Wins / draws / losses - 363 / 8 / 168
Total kills - 50715
Total deaths - 45101
Rounds played - 14083
K/D Ratio - 1.12
Best player(Average rating) - olofmeister (1.15)
Screenshot(我没有代表,要加图,不好意思)
我正在使用 Simple HTML Dom PHP 解析器,我从最基本的开始。为了测试目的,我提取了所有链接,它对我来说效果很好。
include 'simple_html_dom.php';
$url = 'http://www.hltv.org/?pageid=179&teamid=4991&gameid=2';
$html = file_get_html($url);
foreach($html->find('a') as $element) {
echo $element->href . '<br>';
}
$html->clear();
unset($html);
之后,我开始提取主要的 div 块,其中保存了所有内容:
include 'simple_html_dom.php';
$url = 'http://www.hltv.org/?pageid=179&teamid=4991&gameid=2';
$html = file_get_html($url);
foreach ($html->find('div[style="float:right;width:300px;"]') as $div) {
echo $div . '<br/>';
};
它运行良好,结果令人满意 - prntscr。com/88p8l1
然后,我开始越陷越深,陷入困境。
include 'simple_html_dom.php';
$url = 'http://www.hltv.org/?pageid=179&teamid=4991&gameid=2';
$html = file_get_html($url);
foreach ($html->find('div[style="float:right;width:300px;"]') as $div) {
$item['stat-title'] = $html->find('div[style="height:22px;background-color:white"]')->plaintext;
$item['stat-data'] = $html->find('div[style="height:22px;background-color:white"]')->plaintext;
$items[] = $item;
};
print_r($items);
至此,我真的很纠结,如何显示我需要的结果。
我单独测试了代码的一部分 - 它工作正常。
foreach ($html->find('div[style="height:22px;background-color:#E6E5E5"]') as $div) {
echo $div . '<br/>';
};
我想要达到的结果:
<div class="stat">
<span class="stat-title">Wins / draws / losses</span>
<span class="stat-data">363 / 8 / 168</span>
</div>
我需要对我当前的问题有新的看法。提前谢谢你。
$item;
foreach ($html->find('div.covGroupBoxContent div.covSmallHeadline') as $div) {
if(isset($div->style) && $div->style=="font-weight:normal;width:180px;float:left;color:black;text-align:right;") {
//select black text which is the stat data
$item["stat-data"] = $div->plaintext;
//the previous sibling of the data is the title (based on the website)
$item["stat-title"] = $div->prev_sibling()->plaintext;
$items[] = item;
}
};
希望这对您有所帮助。请正确定义问题。