繁体   English   中英

PHP简单的HTML DOM解析器:保存Dom树

[英]PHP Simple HTML DOM Parser: Saving Dom tree

我使用PHP代码使用PHP Simple HTML DOM Parser解析网站,然后在htm文件中保存多个div 我遇到的问题是保存DOM树 由于一时的它正在使用FILE_PUT_CONTENTS保存,并有权使用FILE_APPEND使div的不会写上对方。 但这是一个问题,因为每次运行PHP代码时,它都会向我不想要的文件添加更多内容。 我看了几个选项,但无法理解它。 我希望你能帮助我,谢谢你的时间。

下面是我目前的PHP代码

include( 'site/simple_html_dom.php'); 
$html = file_get_html('http://roosterteeth.com/home.php');
foreach ($html->find('div.streamIndividual') as $div) 
{
file_put_contents('site/test.htm', $div, FILE_APPEND | LOCK_EX);
} 

下面是我编辑PHP的 HTML代码

<?php
include( 'site/simple_html_dom.php'); 
$html = file_get_html('http://roosterteeth.com/home.php');
$divContents = array();
foreach ($html->find('div.streamIndividual') as $div)
{
$divContents[] = $div->outertext;
}
file_put_contents('site/test.htm', implode(PHP_EOL, $divContents));
?>
<script type="text/javascript" src="site/jquery.js"></script>
<script type="text/javascript">
$('document').ready(function() {
$('#wrap').load('site/test.htm');
});
</script>
</head>
<body>
<div id="wrap">
</div>
</body>

以下是test.htm的链接

http://roosterteeth.t15.org/site/test.htm

我仍然使用->outertext ,但只是将内容保存到数组,然后你可以file_put_contents爆数组上使用file_put_contents ,因此用所有 div覆盖文件:

<?php
    include( 'site/simple_html_dom.php'); 
    $html = file_get_html('http://roosterteeth.com/home.php');

    $divContents = array();

    foreach ($html->find('div.streamIndividual') as $div) 
    {
        $divContents[] = $div->outertext;
    }

    file_put_contents('site/test.htm', implode(PHP_EOL, $divContents));
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM