PHP 网页爬虫
只能爬一个页面
<?php
function get_urls($url){ $url_array=array(); $the_first_content=file_get_contents($url); $the_second_content=file_get_contents($url); $pattern1 = "/http:\/\/[a-zA-Z0-9\.\?\/\-\=\&\:\+\-\_\'\"]+/"; $pattern2="/http:\/\/[a-zA-Z0-9\.]+/"; preg_match_all($pattern2, $the_second_content, $matches2); preg_match_all($pattern1, $the_first_content, $matches1); $new_array1=array_unique($matches1[0]); $new_array2=array_unique($matches2[0]); $final_array=array_merge($new_array1,$new_array2); $final_array=array_unique($final_array); for($i=0;$i<count($final_array);$i++) { echo $final_array[$i]."<br/>"; } } get_urls("http://www.yinghy.com"); ?>
<?php $string = GetHtmlCode("http://www.yinghy.com");
echo $string; function GetHtmlCode($url){
$ch = curl_init();//初始化一个cur对象
curl_setopt ($ch, CURLOPT_URL, $url);//设置需要抓取的网页
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);//设置crul参数,要求结果保存到字符串中还是输出到屏幕上
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,1000);//设置链接延迟
$HtmlCode = curl_exec($ch);//运行curl,请求网页
return $HtmlCode;
}
function GetAllLink($string) {
$string = str_replace("\r","",$string);
$string = str_replace("\n","",$string);
$regex[url] = "((http|https|ftp|telnet|news):\/\/)?([a-z0-9_\-\/\.]+\.[][a-z0-9:;&#@=_~%\?\/\.\,\+\-]+)";
$regex[email] = "([a-z0-9_\-]+)@([a-z0-9_\-]+\.[a-z0-9\-\._\-]+)"; //去掉网页中的[]
$string = eregi_replace("\[|\]","",$string); //去掉JAVASCRIPT代码
$string = eregi_replace("<!--.*//-->","", $string); //去掉非<a>的HTML标签
$string = eregi_replace("</?[^aA][^<>]*>","",$string);
//分割$string中的所有链接
$output = split('</a>', $string);
for($i=0; $i<count($output); $i++){
$output_1 = split("<a", $output[$i]);
}
return $output_1;
}
function GetUserCareNews ($test,$keywords,$url) {
$messTxt = "";
$k=0;
$key = explode(";",$keywords); //自动为网站加载上http,避免网易邮箱链接错误,有一定的局限性
if(!ereg("http",$url)){
$url = "http://".$url;
} for($i=0; $i<count($test); $i++){
$test[$i] = eval('return'.iconv('gbk','utf-8',var_export($test[$i],true)).';');//修改编码
if(ereg("href", $test[$i]) && !ereg("href='#'",$test[$i])){//去掉无效链接
for($j=0; $j<count($key); $j++){ //支持多关键字
if(strpos($test[$i],$key[$j])!==false){
$mess[$k++]=ereg_replace($key[$j],"<font color=red>".$key[$j]."</font>", $test[$i]);//高亮关键字
}
}
}
}
$mess = array_unique($mess); //数组去重 //处理好发送链接,为链接加上网站根目录
for($l=0; $l<count($mess); $l++){
if(!ereg("http",$mess[$l]) && (strlen($mess[$l]) != 0)){//去掉空数组,这步很重要,如果不去掉直接影响后面链接的质量
$mess[$l] = eregi_replace("href=[\"']","",$mess[$l]);
$mess[$l] = $url.$mess[$l];
$mess[$l] = eregi_replace(" /","/",$mess[$l]);
if(ereg("'",$mess[$l])){
$mess[$l]="<a href='".$mess[$l]."</a>";
}
if(ereg("\"",$mess[$l])){
$mess[$l] = "<a href=\"".$mess[$l]."</a>";
}
}
else{
$mess[$l] = "<a ".$mess[$l]."</a>";
}
$messTxt .= $mess[$l];
$messTxt .= "<BR>";
}
return $messTxt;
} function SendEmail($to, $content) {
//Author:luofei
//$to 表示收件人地址,$content表示邮件正文内容 error_reporting(E_STRICT); //错误报告
date_default_timezone_set("Asia/Shanghai"); //设定时区 require_once("class.phpmailer.php");
require_once("class.smtp.php"); $mail = new PHPMailer(); //新建一个对象
$mail->CharSet = "UTF-8"; //设置编码,中文不会出现乱码的情况
$mail->IsSMTP(); //设定使用SMTP服务
$mail->SMTPDebug = 1; //启用SMTP调试功能i
//1 = errors and messages
//2 = messages only $mail->SMTPSecure = "tls"; //安全协议
$mail->Host = "smtp.googlemail.com"; //SMTP服务器
$mail->SMTPAuth = true; //启用SMTP验证功能
$mail->Username = "username@gmail.com"; //SMTP服务器用户名
$mail->Password = "******"; //SMTP服务器用户密码 $mail->From = "username@gmail.com"; //发件人
$mail->FromName = "Spider Service"; //发件人姓名(邮件上显示) $mail->AddAddress($to); //收件人地址
$mail->WordWrap = 50; //设置邮件正文每行的字符数
$mail->IsHTML(true); //设置邮件正文内容是否为html类型 $mail->Subject = "来自spider.html的邮件"; //邮件主题
$mail->Body = "<p>您好!<BR> <p>这是您感兴趣的内容</p> <BR>".$content." ";
//邮件正文
if(!$mail->Send()) //邮件发送报告
{
echo "发送邮件错误!";
}
else
{
echo "邮件发送成功!";
}
} ?>
PHP 网页爬虫的更多相关文章
- cURL 学习笔记与总结(2)网页爬虫、天气预报
例1.一个简单的 curl 获取百度 html 的爬虫程序(crawler): spider.php <?php /* 获取百度html的简单网页爬虫 */ $curl = curl_init( ...
- c#网页爬虫初探
一个简单的网页爬虫例子! html代码: <head runat="server"> <title>c#爬网</title> </head ...
- 网页爬虫--scrapy入门
本篇从实际出发,展示如何用网页爬虫.并介绍一个流行的爬虫框架~ 1. 网页爬虫的过程 所谓网页爬虫,就是模拟浏览器的行为访问网站,从而获得网页信息的程序.正因为是程序,所以获得网页的速度可以轻易超过单 ...
- 网页爬虫的设计与实现(Java版)
网页爬虫的设计与实现(Java版) 最近为了练手而且对网页爬虫也挺感兴趣,决定自己写一个网页爬虫程序. 首先看看爬虫都应该有哪些功能. 内容来自(http://www.ibm.com/deve ...
- Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱(转)
原文:http://www.52nlp.cn/python-网页爬虫-文本处理-科学计算-机器学习-数据挖掘 曾经因为NLTK的缘故开始学习Python,之后渐渐成为我工作中的第一辅助脚本语言,虽然开 ...
- [resource-]Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱
reference: http://www.52nlp.cn/python-%e7%bd%91%e9%a1%b5%e7%88%ac%e8%99%ab-%e6%96%87%e6%9c%ac%e5%a4% ...
- 网页抓取:PHP实现网页爬虫方式小结
来源:http://www.ido321.com/1158.html 抓取某一个网页中的内容,需要对DOM树进行解析,找到指定节点后,再抓取我们需要的内容,过程有点繁琐.LZ总结了几种常用的.易于实现 ...
- Java正则表达式--网页爬虫
网页爬虫:其实就一个程序用于在互联网中获取符合指定规则的数据 爬取邮箱地址,爬取的源不同,本地爬取或者是网络爬取 (1)爬取本地数据: public static List<String> ...
- 从robots.txt開始网页爬虫之旅
做个网页爬虫或搜索引擎(下面统称蜘蛛程序)的各位一定不会陌生,在爬虫或搜索引擎訪问站点的时候查看的第一个文件就是robots.txt了.robots.txt文件告诉蜘蛛程序在server上什么文件是能 ...
- Python网页爬虫(一)
很多时候我们想要获得网站的数据,但是网站并没有提供相应的API调用,这时候应该怎么办呢?还有的时候我们需要模拟人的一些行为,例如点击网页上的按钮等,又有什么好的解决方法吗?这些正是python和网页爬 ...
随机推荐
- WPF学习笔记-自定义窗口
代码部分 <Style x:Key="for_noresize_window" TargetType="{x:Type Window}"> < ...
- Java中接口与实例化
一.问题引入 前两天学代理模式的时候想到的,接口可不可以new呢? 接口是特殊的抽象类,接口的方法都默认为 public abstract 的... 抽象的方法不 ...
- Git skills in reseting files
How to uncommit files that were committed long time a ago?Note: Since all changes in the current wor ...
- n数码问题, 全排列哈希
转载了一篇关于全排列的哈希函数,Poj1077就是应用了全排列的哈希: 我们经常使用的数的进制为“常数进制”,即始终逢p进1.例如,p进制数K可表示为 K = a0*p^0 + a1*p^1 + ...
- Blue Jeans(串)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10083 Accepted: 4262 Description The ...
- Vim笔记
复制多行: v 可视化,选择多行,然后yy复制.
- Summary Ranges —— LeetCode
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- 《Mathematical Olympiad——组合数学》——染色问题
恢复 继续关于<Mathematical Olympiad——组合数学>中问题的分析,这一篇文章将介绍有关染色的问题. 问题一: 将一些石头放入10行14列的矩形方格表内,允许在每个单元 ...
- [转载]Linux服务器性能评估与优化
转载自:Linux服务器性能评估与优化 一.影响Linux服务器性能的因素 1. 操作系统级 CPU 内存 磁盘I/O带宽 网络I/O带宽 2. 程序应用级 二.系统性能评估标准 影响性 ...
- Servlet的接口实现类
Sun公司提供了两个默认实现类 GenericServlet和HttpServlet HttpServlet指能够处理Http请求的Servlet,它在原有的Servlet基础上添加了与HTTp相关的 ...