【问题标题】:PHP Scraping and storing output locallyPHP 在本地抓取和存储输出
【发布时间】:2014-04-01 17:32:06
【问题描述】:

我有一个小的 PHP 脚本从 atom 提要中提取信息。然后将该回声推送到文件或数据库中的最佳方法是什么?我希望能够按小时参考该信息。

例如。

PHP grabs HTML page.
Webserver stores the resulting HTML locally.
Webserver references that locally stored HTML.
Webserver displays a template version of the local information.

这是拉取 html 的 php:

<?php include('simple_html_dom.php'); echo file_get_html('http://www.website.com')->plaintext[1] ; ?>

【问题讨论】:

    标签: php html storage scrape


    【解决方案1】:

    你可以使用file_put_contents(),这很简单。 http://php.net/file_put_contents

    file_put_contents("myFile", file_get_html('http://www.website.com')->plaintext[1]);
    

    【讨论】:

    • 你太棒了,只要我可以给代表,我就会来投票给你。
    【解决方案2】:

    希望这会有所帮助...

    <html>
    <head>
    <title>Online PHP Script Execution</title>
    </head>
    <body>
    <?php require'simple_html_dom.php'; ?>
    <?php
        $url_path='http://www.google.com/';
        $create=fopen("folder/"."index.html",'w+') or die("can't open file");
        $src=file_get_html($url_path)->plaintext ;
        $write=fwrite($create,$src);
            fclose($create);
    
        foreach($html->find('script') as $script)   //get script 
                {
    
                        $scriptPath=$script->src;
                        $js = explode("/", $scriptPath);
                        $jsName = end($js);
                 $path="";
                      for ($i=0;$i<(count($image)-1);$i++) 
                          {
                            $path .= $image[$i] . "/";
                            if(!file_exists('folder/'.$path))
                                {
                                    mkdir('folder/'.$path, 0777, true);     //jquery folder created
                                }       
                          }
                        file_put_contents('folder/'.$path.$jsName,file_get_contents($url_path.$scriptPath)); //all jquery downloaded
                }
    
            foreach($html->find('img') as $img)     //image
                    {
    
                        $imgpath=$img->src;
                        $image = explode("/", $imgpath);
              $path="";
                          for ($i=0;$i<(count($image)-1);$i++) 
                          {
                            $path .= $image[$i] . "/";
                            if(!file_exists('folder/'.$path))
                                {
                                    mkdir('folder/'.$path, 0777, true);     //img folder created
                                }       
                          }
    
                        $imgName = end($image);
                            file_put_contents('folder/'.$path.$imgName,file_get_contents($url_path.$imgpath));  //img downloaded
    
                    } 
    
            foreach($html->find('link') as $link)  //get link 
                    {
                            if(strtolower($link->getAttribute('rel')) == "stylesheet" ) 
                            {
                                $linkpath=$link->getAttribute('href');
                            }
    
                            $links = explode("/", $linkpath);
                            $linkName = end($links);
                          $path="";
                      for ($i=0;$i<(count($image)-1);$i++) 
                          {
                            $path .= $image[$i] . "/";
                            if(!file_exists('folder/'.$path))
                                {
                                    mkdir('folder/'.$path, 0777, true);     //css folder created
                                }       
                          }
    
                                file_put_contents('folder/'.$path.$linkName,file_get_contents($url_path.$linkpath));    //download css
                            }
    ?>
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-10
      • 1970-01-01
      • 2022-01-24
      相关资源
      最近更新 更多