参考文档1

参考文档2

这里是我自己写的一个小需求

<?php
/**
采集http://www.959.cn/school,即时更新的最新的文章内容;每分钟采集一次。采集结束后实时入库并展示。
*/
header("Content-Type: text/html;charset=utf-8");
date_default_timezone_set('PRC');
$con = mysqli_connect('127.0.0.1', 'xxxxx', 'xxxxx', 'xxxxx');
mysqli_set_charset($con,'UTF8'); // 日志位置
//把当前的最新一条文章的id.shtml的id 存入一个max.log的文件中,实时读取 实时更改
$log_path = '/home/logs';
if(!file_exists($log_path)){
mkdir($log_path,0755,true);
}
$max_log = $log_path.'/kecheng_max.log';
$path = './img/';
$max = file_get_contents($max_log) ? file_get_contents($max_log) : 4613925; //当前的最新一条文章的id.shtml的id //记录日志
$message_log = $log_path.'/kecheng_message.log'; $now_max = 0; //当前网站实际的最新一条的文章的id.shtml的id
$url = "http://www.959.cn/school/kecheng/";//要爬取的网址
$res = file_get_contents("compress.zlib://".$url);//curl封装方法
//var_dump($con);die;
preg_match_all('/<h3>.*<\/h3>/',$res,$arr_all);//文章列表内容
//var_dump($arr_all[0]);
foreach($arr_all[0] as $k => $v){
// 先去匹配a标签 如果不是a标签直接继续下一个
preg_match_all('/<a href="(.*)">.*<\/a>/',$v,$v_all);
if (empty($v_all[1])){// 不是a标签
continue;
} $title = strip_tags($v_all[0][0]); //文章标题
$href = $v_all[1][0];//url // 截取当前url的shtml前面的id
$href_one = substr($href, strrpos($href, '/')+1);
$href_id = substr($href_one, 0, strpos($href_one, '.')); if ($k === 0) {
$now_max = $href_id;
} if ($href_id <= $max){ // 只爬大于保存的最大id的数据
$max = file_put_contents($max_log, $now_max, LOCK_EX); // 爬完之后让maxid更新为最新的
break;
} // 获取标题缩略图
preg_match_all('/<img src="(.*)" alt="'.$title.'" \/>/',$res,$title_img);
$title_img_url = $title_img[1][0];
if(!file_exists($path)){
mkdir($path,0755,true);
}
$title_img = file_get_contents ("http:{$title_img_url}");
$cover_img_name = microtime_float().'.jpg';
file_put_contents($path.$cover_img_name, $title_img);
$cover_img = '/public/uploads/img/'.date('Y/md',time()).'/'.$cover_img_name; // 标题缩略图位置 // 获取文章内容
$article_res = file_get_contents("compress.zlib://".$href);
//var_dump($article_res);die;
$article_res = trimall($article_res);
//var_dump($article_res);die;
preg_match_all('/<div class="detail">(.*?)<\/div>/',$article_res,$article_all);//文章详情内容 $text = $article_all[1][0]; // 文章内容
//var_dump($text);die; //筛选图片 并下载到本地
preg_match_all('/<img .*? src="(.*?)" .*? \/>/',$article_all[1][0],$img_all); //文章详情图片
//preg_match_all('/<imgalt="创业项目"src="(.*?)"title="创业项目" \/>/',$article_all[0][0],$img_all); //文章详情图片
//var_dump($img_all[1]);
foreach($img_all[1] as $key => $value){
$message_img = file_get_contents ("http:{$value}");
$message_img_name = microtime_float().'.jpg';
file_put_contents($path.$message_img_name, $message_img); $text = str_replace($value,'/public/uploads/img/'.date('Y/md',time()).'/'.$message_img_name,$text);
}
// 去掉【如果还有什么问题点击留言咨询】 和 >>>>想了解更多创业资讯,点击咨询详情 和 其他的a标签
$text = preg_replace("/<a[^>]*>(.*?)<\/a>/is", "", $text);
//var_dump($text);die; // 入库数据
$cid = 2;// 1:行业资讯,2:创业故事
$source = '88加盟网';
$create_time = time();
$update_time = time(); // 入库前转码
$title = mb_convert_encoding($title,'UTF-8');
$text = mb_convert_encoding($text,'UTF-8');
//var_dump($title, $text); // 插入数据库
$sql = "insert into zs_message (cid, title, create_time, update_time, source, cover_img, text) values ($cid, '$title', $create_time, $update_time, '$source', '$cover_img', '$text')";
$result = mysqli_query($con,$sql); if ($result) {
$msg = "当前抓取的页面url为:{$href},入库sql为:{$sql},结果:入库成功";
} else {
$msg = "当前抓取的页面url为:{$href},入库sql为:{$sql},结果:入库失败";
}
file_put_contents($message_log, $msg, FILE_APPEND | LOCK_EX);
file_put_contents($message_log, "\n\r", FILE_APPEND | LOCK_EX); } function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return $sec.substr($usec,2,6);
} function trimall($str){
$qian=array("\n","\r");
return str_replace($qian, '', $str);
} function curl_get_contents($url,$cookie='',$referer='',$timeout=300,$ishead=0) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_TIMEOUT,$timeout);
curl_setopt($curl, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36');
if($cookie)
{
curl_setopt( $curl, CURLOPT_COOKIE,$cookie);
}
if($referer)
{
curl_setopt ($curl,CURLOPT_REFERER,$referer);
}
$ssl = substr($url, 0, 8) == "https://" ? TRUE : FALSE;
if ($ssl)
{
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
}
$res = curl_exec($curl);
return $res;
curl_close($curl);
}

爬虫主要的思路是:用正则去筛选我们爬取我们的页面,然后从列表页获取到的url,再一个个去爬取详情页的内容

注意事项:

  1. 如果用file_get_contens去获取内容的话,gzip压缩,会出现乱码的情况
file_get_contents("compress.zlib://".$url);

如是用的curl的话

function curl_get($url, $gzip=false){
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
if($gzip) curl_setopt($curl, CURLOPT_ENCODING, "gzip"); // 关键在这里
$content = curl_exec($curl);
curl_close($curl);
return $content;
}

无论页面是否经过gzip压缩,上述代码都可以正常工作!

参考出处

2.在获取到页面后,在匹配之前,一定要先把字符串中的\r\n空格换行都去掉,在进行匹配,否则会出现匹配为空的情况

function trimall($str){
$qian=array("\n","\r");
return str_replace($qian, '', $str);
}
// 处理页面源码,多行变单行
$htmlOneLine = preg_replace("/\r|\n|\t/","",$html);

3.去掉文章中的超链接或者将锚文本,只保留文字,去掉链接,去掉加粗格式

$text = preg_replace("/<a[^>]*>(.*?)<\/a>/is", "", $text);//去掉文章中的超链接
$str = preg_replace("/<a[^>]*>(.*?)<\/a>/is", "$1", $str);//只保留文字

4.生成图片的路径和文件名(参考)

$cover_img_name = microtime_float().'.jpg';
$cover_img = '/public/uploads/img/'.date('Y/md',time()).'/'.$cover_img_name;
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return $sec.substr($usec,2,6);
}

5.入库的时候,有可能会出现,存入的数据读出来全是???,或者乱码 要把页面设置为utf8,数据库设置为utf8

header("Content-Type: text/html;charset=utf-8");
date_default_timezone_set('PRC');
$con = mysqli_connect('127.0.0.1', 'xx', 'xx', 'xx');
mysqli_set_charset($con,'UTF8'); // 入库前转码
$title = mb_convert_encoding($title,'UTF-8');
$text = mb_convert_encoding($text,'UTF-8');

用php写爬虫去爬数据的更多相关文章

  1. 开发记录_自学Python写爬虫程序爬取csdn个人博客信息

    每天刷开csdn的博客,看到一整个页面,其实对我而言,我只想看看访问量有没有上涨而已... 于是萌生了一个想法: 想写一个爬虫程序把csdn博客上边的访问量和评论数都爬下来. 打算通过网络各种搜集资料 ...

  2. 使用selenium爬虫抓取数据

    写在前面 本来这篇文章该几个月前写的,后来忙着忙着就给忘记了.ps:事多有时候反倒会耽误事.几个月前,记得群里一朋友说想用selenium去爬数据,关于爬数据,一般是模拟访问某些固定网站,将自己关注的 ...

  3. 初次尝试python爬虫,爬取小说网站的小说。

    本次是小阿鹏,第一次通过python爬虫去爬一个小说网站的小说. 下面直接上菜. 1.首先我需要导入相应的包,这里我采用了第三方模块的架包,requests.requests是python实现的简单易 ...

  4. 怎么用Python写爬虫抓取网页数据

    机器学习首先面临的一个问题就是准备数据,数据的来源大概有这么几种:公司积累数据,购买,交换,政府机构及企业公开的数据,通过爬虫从网上抓取.本篇介绍怎么写一个爬虫从网上抓取公开的数据. 很多语言都可以写 ...

  5. python爬虫25 | 爬取下来的数据怎么保存? CSV 了解一下

    大家好 我是小帅b 是一个练习时长两年半的练习生 喜欢 唱! 跳! rap! 篮球! 敲代码! 装逼! 不好意思 我又走错片场了 接下来的几篇文章 小帅b将告诉你 如何将你爬取到的数据保存下来 有文本 ...

  6. Python写爬虫-爬甘农大学校新闻

    Python写网络爬虫(一) 关于Python: 学过C. 学过C++. 最后还是学Java来吃饭. 一直在Java的小世界里混迹. 有句话说: "Life is short, you ne ...

  7. 爬虫爬数据时,post数据乱码解决办法

    最近在写一个爬虫,目标网站是:http://zx.bjmemc.com.cn/,可能是为了防止被爬取数据,它给自身数据加了密.用谷歌自带的抓包工具也不能捕获到数据.于是下了Fiddler.     F ...

  8. python爬虫16 | 你,快去试试用多进程的方式重新去爬取豆瓣上的电影

    我们在之前的文章谈到了高效爬虫 在 python 中 多线程下的 GIL 锁会让多线程显得有点鸡肋 特别是在 CPU 密集型的代码下 多线程被 GIL 锁搞得效率不高 特别是对于多核的 CPU 来说 ...

  9. 爬虫爬数据时,post数据乱码解决的方法

    近期在写一个爬虫,目标站点是:http://zx.bjmemc.com.cn/.可能是为了防止被爬取数据,它给自身数据加了密. 用谷歌自带的抓包工具也不能捕获到数据. 于是下了Fiddler.     ...

随机推荐

  1. mysql 用户及权限管理 允许远程连接

    mysq,功能强大的关系型数据库,它的用户管理在开发过程中当然也尤其重要,接下来就看看mysql的用户管理 1.登录数据库 mysql -uroot -p 回车 输入密码... 回车 2.登录成功后, ...

  2. Codefores 507B Amr and Pins

    B. Amr and Pins time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  3. SqlServer表名称定义

    每一个数据表 添加一个 扩展 属性:Description  填写表描述. 查看是否所有表都添加的Sql如下: SELECT a.name AS name, g.[value] FROM sys.ta ...

  4. Java8 LocalDate操作时间和日期的API

    时间项目中的涉及到的时间处理非常多,犹豫SimpleDateFormat的不安全性以及Calendar等类在计算时比较复杂, 往往我们都会使用工具类来封装较多的日期处理函数, 但是JDK8中新增了操作 ...

  5. ng2-file-upload插件在ionic3中的使用方法

    本文主要说明在ionic3中使用ng2-file-upload插件,上传多种类型文件(图片.word.excel.ppt.mp4等)的使用方法. 1.html代码: <button ion-bu ...

  6. 【知识强化】第六章 查找 6.4 散列(Hash)表

    本节课我们来学习一种新的查找方式叫做散列查找.什么是散列查找呢?在学习散列查找之前,一定要介绍一个基本概念就是散列表.那么学习散列表之前我们先来回忆一下之前所学习过的所有查找方式,那么无论是顺序查找还 ...

  7. 2018-8-10-WPF-判断USB插拔

    title author date CreateTime categories WPF 判断USB插拔 lindexi 2018-08-10 19:16:53 +0800 2018-8-5 13:0: ...

  8. Bootstrap 小功能

    <div class="container"> <div class="navbar-header"> <button type= ...

  9. React 滚动事件

    代码: <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UT ...

  10. function的各做写法

    function(){}()让变量快速初始化结果 var timestamp = function(){ var timestamp = Date.parse(new Date()); return ...