试图使用PHP解析网页

时间:2013-11-12 03:51:56

标签: javascript php html dom web-crawler

我正在尝试解析网页并打印出网页上的表格。我正在使用php_simple_html dom解析器。但是,当我尝试从网页上解析表格时,输出表格的所有javascript命令都会变成php中的注释:

<html>
<script type="text/javascript" src="jquery.js"></script>
<?php
    include 'crawling/simple_html_dom.php';
    $html = file_get_html('http://uiucfreefood.com/');


    $ret = $html->find('body', 0)->find('div', 10)->find('table',0); //gets to the table tag
    echo $ret; // nothing is echoed out because the original webpage uses jscript commands to write the table to the page but these commands get turned to comments for some reason.
?>
</html>

当我检查我正在回显解析信息的页面元素时,我能够看到包含所有信息的表标记在那里,但是jscript命令已经变成了注释。有没有办法让我抓住信息并自己回应?我尝试添加另一个 - &gt; find('tbody');在parse命令的末尾但它没有做任何事情。任何建议表示赞赏。感谢。

编辑:如果您下载simple_html_dom.php并将其包含在您的php文件中,您可以自己尝试使用此代码。资料来源:http://sourceforge.net/projects/simplehtmldom/files/

编辑:刚刚注意到一些非常重要的事情。 javascript命令也在原始网页中注释掉了。相反,原始网页使用javascript函数打印出我没有定义的表。自己写这个功能应该解决这个问题。

编辑:是的,有效。

1 个答案:

答案 0 :(得分:2)

尝试使用file_get_content而不是获取HTML并查看是否有效。老实说,根据您的需要,您应该编写自己的解析器代码。为表扫描和显示编写解析器并不难。

您只需要以下内容;

$array = split("<table>", $content);
$boolPlaceHolder = false;

然后您可以在遇到这种方式时将占位符设置为true,您可以浏览内容的字符并抓住表格。

希望这有帮助。