【发布时间】:2015-06-15 14:28:17
【问题描述】:
我想从外部页面解析一些数据(游戏名称): https://www.mol.com/Product/GamesHive
使用此代码:
<?php
$url = 'https://www.mol.com/Product/GamesHive';
$content = file_get_contents($url);
$first_step = explode('<div class="col-xs-4">', $content);
$second_step = explode('</div>', $first_step[1]);
echo $second_step[0];
?>
但是有些数据丢失了。原始页面有384 项目,我的页面只有169 项目。有什么问题?
【问题讨论】:
-
因为某些元素可能包含额外的
class或id -
问题是当
</div>或<div class="col-xs-4">发现将被删除。 -
不要在 html 上使用这样的字符串操作。并非所有的“col-xs-4”标签都是这样的,例如
<div class="col-xs-4 xs-margin-bottom-40">会被你的爆炸遗漏。使用 DOM 解析器和 xpath 来获取您想要的任何内容。 -
我认为结果和用户国家有关系!