很多问题,伙计们,但我真的需要你的帮助,让我走上正确的道路。
非常感谢。
发布于 2010-05-04 18:06:39
很简单,试试我为你做的代码。
获取并保存DB中的源代码是最基本的。
我没有把错误处理或者别的什么都放进去,暂时保持简单.
我没有创建函数来显示结果,但是您可以打印$source来查看结果。
希望这能帮到你。
<?php
function GetPage($URL)
{
#Get the source content of the URL
$source = file_get_contents($URL);
#Extract the raw URl from the current one
$scheme = parse_url($URL, PHP_URL_SCHEME); //Ex: http
$host = parse_url($URL, PHP_URL_HOST); //Ex: www.google.com
$raw_url = $scheme . '://' . $host; //Ex: http://www.google.com
#Replace the relative link by an absolute one
$relative = array();
$absolute = array();
#String to search
$relative[0] = '/src="\//';
$relative[1] = '/href="\//';
#String to remplace by
$absolute[0] = 'src="' . $raw_url . '/';
$absolute[1] = 'href="' . $raw_url . '/';
$source = preg_replace($relative, $absolute, $source); //Ex: src="/image/google.png" to src="http://www.google.com/image/google.png"
return $source;
}
function SaveToDB($source)
{
#Connect to the DB
$db = mysql_connect('localhost', 'root', '');
#Select the DB name
mysql_select_db('test');
#Ask for UTF-8 encoding
mysql_query("SET NAMES 'utf8'");
#Escape special chars
$source = mysql_real_escape_string($source);
#Set the Query
$query = "INSERT INTO website (source) VALUES ('$source')"; //Save it in a text row, that's it...
#Run the query
mysql_query($query);
#Close the connection
mysql_close($db);
}
$source = GetPage('http://www.google.com');
SaveToDB($source);
?>发布于 2010-05-03 21:57:27
使用fopen拉下整个页面,并解析出任何URL(比如图像和css)。您需要运行一个循环来获取生成页面的文件的每个urls。还可以存储这些文件,并用新链接替换用于链接到其他站点文件的urls。(这将避免任何问题,如果文件应该改变或删除在未来)。
我建议使用blob数据类型,因为它允许将所有文件存储在一个表中,但是您可以使用文本数据类型为页面创建一个表,用blob为另一个页面创建一个表来存储图像和其他文件。
编辑:如果以blob数据类型存储,查看base64_encode()将增加服务器上的存储空间,但您将避免引号和特殊字符的任何问题。
发布于 2010-05-04 13:42:56
不要使用关系数据库来存储文件。使用文件系统或NoSQL解决方案。
您可能需要查看各种可用的开源蜘蛛(注意到htdig和httrack )。
https://stackoverflow.com/questions/2761191
复制相似问题