php简单的抓取
php抓取当前页面中所有链接,再分析下载想要下载的链接,保存到数据库

<?PHP
// GET all links from URL
function remove_html_tags(&$item, $key)
{
$item=trim(strip_tags($item));
}
function get_path($url)
{
preg_match("/^(http:\/\/)?([^\/]+)/i", $url,$matches);
return substr($url, strlen($matches[0]));
}
function get_host($url)
{
preg_match("/^(http:\/\/)?([^\/]+)/i", $url,$matches);
return $matches[2];
}
function get_all_html($url)
{
$htmlString = '';
$host = get_host($url);
$path = get_path($url);
$fp = fsockopen($host, 80, $errno, $errstr, 30);
if (! $fp)
{
echo "$errstr ($errno)<br />\n";
} else
{
$out = "GET $path HTTP/1.1\r\n";
$out .= "Host: $host \r\n";
$out .= "Connection: Close\r\n\r\n";
$myInt = fwrite($fp, $out);
while (! feof($fp))
{
$htmlString = $htmlString.fgets($fp);
}
fclose($fp);
}
return $htmlString;
}
function get_links($url)
{
$preg =
"/a[\s]+[^>]*?href[\s]?=[\s\"\']+(.*?)[\"\']+.*?>"
."([^<]+|.*?)?<\/a>/i";
preg_match_all(trim($preg),
get_all_html($url), $out, PREG_PATTERN_ORDER);
$keys = $out[1];
$values = $out[2];
array_walk($values, 'remove_html_tags');
return (array_combine($keys, $values));
}
//过滤关键字 不包含特定的字符串 得到想要下载的链接
function filter_string($var)
{
$pos = strpos($var, '中日研修');//只想要 含中日研修的链接
if ($pos === false)
return false;
else
return true;
}
//保存到数据库
function save_html($title, $html)
{
$link = mysql_connect("localhost:3306","root","")OR die(mysql_error());
$res = mysql_select_db("test", $link);
$result = mysql_query("insert into save_info(title, content) values ('$title', '$html')",$link);
return $result;
}
$url = "http://www.zggjlww.cn/share/?mods=news&action=list&id=15";
$arr = get_links($url);
$newMatches = array_filter($arr,"filter_string");
//提取a 中的 href内容 合成完整的url, 连合标题保存到二维数组中
//$strLinks = implode($newMatches,'<p></p>');
//print_r($newMatches);
//foreach所操作的是指定数组的一个拷贝,而不是该数组本身。 每次指向数组的第一位
//foreach ($newMatches as $url => $site)
//{
// echo "<br /> 址址: $site, url: $url";
//}
/*
* reset($newMatches);
* while (list($key, $val) = each($newMatches))
* {
* echo "$key => $val\n";
* }
*/
reset($newMatches);
$count = 1;
while(list($key,$val) = each($newMatches))
{
$key = "http://" . get_host($url) . $key;
$html = substr(htmlentities(get_all_html($key)), 2000, 10000);//我抓的页面太大了,截断了点了才能保存到mysql
save_html($val,$html);
$rest = count($newMatches) - $count;
$count++ ;
echo "$val = > $key <br />保存成功... 还有 $rest 张页面要处理...<p></p>";
}
?>
// GET all links from URL
function remove_html_tags(&$item, $key)
{
$item=trim(strip_tags($item));
}
function get_path($url)
{
preg_match("/^(http:\/\/)?([^\/]+)/i", $url,$matches);
return substr($url, strlen($matches[0]));
}
function get_host($url)
{
preg_match("/^(http:\/\/)?([^\/]+)/i", $url,$matches);
return $matches[2];
}
function get_all_html($url)
{
$htmlString = '';
$host = get_host($url);
$path = get_path($url);
$fp = fsockopen($host, 80, $errno, $errstr, 30);
if (! $fp)
{
echo "$errstr ($errno)<br />\n";
} else
{
$out = "GET $path HTTP/1.1\r\n";
$out .= "Host: $host \r\n";
$out .= "Connection: Close\r\n\r\n";
$myInt = fwrite($fp, $out);
while (! feof($fp))
{
$htmlString = $htmlString.fgets($fp);
}
fclose($fp);
}
return $htmlString;
}
function get_links($url)
{
$preg =
"/a[\s]+[^>]*?href[\s]?=[\s\"\']+(.*?)[\"\']+.*?>"
."([^<]+|.*?)?<\/a>/i";
preg_match_all(trim($preg),
get_all_html($url), $out, PREG_PATTERN_ORDER);
$keys = $out[1];
$values = $out[2];
array_walk($values, 'remove_html_tags');
return (array_combine($keys, $values));
}
//过滤关键字 不包含特定的字符串 得到想要下载的链接
function filter_string($var)
{
$pos = strpos($var, '中日研修');//只想要 含中日研修的链接
if ($pos === false)
return false;
else
return true;
}
//保存到数据库
function save_html($title, $html)
{
$link = mysql_connect("localhost:3306","root","")OR die(mysql_error());
$res = mysql_select_db("test", $link);
$result = mysql_query("insert into save_info(title, content) values ('$title', '$html')",$link);
return $result;
}
$url = "http://www.zggjlww.cn/share/?mods=news&action=list&id=15";
$arr = get_links($url);
$newMatches = array_filter($arr,"filter_string");
//提取a 中的 href内容 合成完整的url, 连合标题保存到二维数组中
//$strLinks = implode($newMatches,'<p></p>');
//print_r($newMatches);
//foreach所操作的是指定数组的一个拷贝,而不是该数组本身。 每次指向数组的第一位
//foreach ($newMatches as $url => $site)
//{
// echo "<br /> 址址: $site, url: $url";
//}
/*
* reset($newMatches);
* while (list($key, $val) = each($newMatches))
* {
* echo "$key => $val\n";
* }
*/
reset($newMatches);
$count = 1;
while(list($key,$val) = each($newMatches))
{
$key = "http://" . get_host($url) . $key;
$html = substr(htmlentities(get_all_html($key)), 2000, 10000);//我抓的页面太大了,截断了点了才能保存到mysql
save_html($val,$html);
$rest = count($newMatches) - $count;
$count++ ;
echo "$val = > $key <br />保存成功... 还有 $rest 张页面要处理...<p></p>";
}
?>
mysql数据库创建脚本
create table save_info
(
id int auto_increment not null,
title text,
content longtext,
primary key(id)
);
分类:
php
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义