2014-01-28 53 views
-3

此脚本从种子URL中获取链接,并仅将它们打印到命令shell(或浏览器)中,而不是保存在别处。我希望脚本将任何输出存储在脚本所在文件夹内的.txt文件中。我需要建议什么可能是有效的方式来做到这一点。请给我提示。需要更改PHP刮板脚本

<?php 

# Initialization 
include("LIB_http.php");      // http library 
include("LIB_parse.php");      // parse library 
include("LIB_resolve_addresses.php");   // address resolution library 
include("LIB_exclusion_list.php");    // list of excluded keywords 
include("LIB_simple_spider.php");    // spider routines used by this app. 


set_time_limit(3600);       // Don't let PHP timeout 

$SEED_URL  = "http://www.schrenk.com"; // First URL spider downloads 
$MAX_PENETRATION = 1;       // Set spider penetration depth 
$FETCH_DELAY  = 1;       // Wait one second between page fetches 
$ALLOW_OFFISTE = false;      // Don't allow spider to roam from the SEED_URL's domain 
$spider_array = array(); 

# Get links from $SEED_URL 
echo "Harvesting Seed URL \n"; 
$temp_link_array = harvest_links($SEED_URL); 
$spider_array = archive_links($spider_array, 0, $temp_link_array); 

# Spider links in remaining penetration levels 
for($penetration_level=1; $penetration_level<=$MAX_PENETRATION; $penetration_level++) 
    { 
    $previous_level = $penetration_level - 1; 
    for($xx=0; $xx<count($spider_array[$previous_level]); $xx++) 
     { 
     unset($temp_link_array); 
     $temp_link_array = harvest_links($spider_array[$previous_level][$xx]); 
     echo "Level=$penetration_level, xx=$xx of ".count($spider_array[$previous_level])." <br>\n"; 
     $spider_array = archive_links($spider_array, $penetration_level, $temp_link_array); 
     } 
    } 

?> 
+0

你的问题是“如何将字符串保存到文件?” - >“ 'file_put_contents()'是你的朋友[和谷歌以及]。 – moonwave99

+0

搜索Ob_Start()和file_put_contents()并告诉我它是否服务于你想要的,我举一个例子 –

+0

#Vinicius file_put_contents()是解决方案I正在寻找。 – user2928990

回答

0

我会建议首先创建一个变量来保存脚本输出。因此,在顶部(下$spider_array=array())地址:

$output = ""; 

改变所有的线用echo$output .=

这将存储所有发送到屏幕或浏览器进入$output变量的内容。

现在在脚本的底部,一切都已经刮和蜘蛛完成后,将输出保存到一个文件:

$filename = date('Y_m_d_H_i_s') . '.txt'; 
$filepath = dirname(__FILE__); 
file_put_contents($filepath . '/' . $filename, $output); 

这应该保存在同一文件夹内的文件输出带日期/时间文件名的脚本。 (这段代码是使用php.net中的示例编写的,确切的实现可能需要一些调试,但这应该让你足够接近。