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和网页爬 ...
随机推荐
- pycharm去掉拼写检查
http://zhidao.baidu.com/question/523436629.html
- Black Box《优先队列》
Description Our Black Box represents a primitive database. It can save an integer array and has a sp ...
- 禁用窗体关闭按钮(使用GetWindowLong修改GWL_STYLE)
一般我们不想让窗体能够关闭, 首先想到的是在OnCloseQuery事件里设置CanClose := False, 不过在某些情况下这个会和程序关闭窗体的业务逻辑产生冲突 所以写了下面这个函数, 可以 ...
- MFC中对话框的工具栏的使用
1.新建一个MFC项目:在资源视图中新建Toolbar资源: 2.编辑状态栏: 3.在***Dlg.h文件中添加CToolBar类型或其派生类型的一个变量如:(CdlgToolBar myToolBa ...
- 构造函数语义学之Default Constructor构建操作
一.Default Constructor的构建操作 首先大家要走出两个误区: 1).任何class如果没有定义default constructor,就会被合成一个来. 2).便以其合成出来的def ...
- Socket(TCP)客户端请求和服务端监听和链接基础(附例子)
一:基础知识回顾 一: Socket 类 实现 Berkeley 套接字接口. Socket(AddressFamily, SocketType,ProtocolType) 使用指定的地址族.套接字类 ...
- Sort Colors —— LeetCode
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- HDOJ(HDU) 2136 Largest prime factor(素数筛选)
Problem Description Everybody knows any number can be combined by the prime number. Now, your task i ...
- Jenkins 一: 环境安装以及配置
安装JDK 下载地址: http://www.oracle.com/technetwork/java/javase/downloads/index.html 选择的JDK版本和开发使用的JDK版本最好 ...
- phantomjs截图的实践
年前也做了一些东西,由于过年只顾疯了,也没有整理,年后补上把~ 今天整理一下个phantomjs截图实践中遇到的问题. 最近做的一个项目,需要在首页展示已经做的一些项目的缩 ...