网上有的朋友在去面试过程中有被要求去做数据采集工作的,但是由于对正则匹配运用的的不熟悉,而陷入了学而无以致用局面尴尬局面;做为新人我自己用所学过的知识尝试着做了一个网站详情页部分信息的数据采集,其中有逻辑不严谨或者应用不恰当的地方欢迎各位大神斧正;
做数据采集的流程:
进入一个网站后首先要查看网页源代码,审查检测源代码中是否存在我们需要的内容,如果有则可以直接使用正则匹配的方式来进行数据的抓取和提取;
假如我们需要的内容是由网站的js动态生成的,我们就不能直接对网站进行正则匹配的方式进行数据抓取,这时我们需要打开(注:我用的火狐)Firebug网络中查看服务器传输回的数据里可以找到服务器返回的源码,反而更加便捷;
在PHP中有一些内置的函数可以帮助我们更快捷的抓取网页源代码比如:file_get_contents; 包括正则匹配里常用的preg_match,preg_match_all;正确并熟练的使用PHP内置函数和方法可以极大节省我们的时间;
class PHP1
{
/**
* 采集详情页
*/
public function getDetailPage($url)
{
//获取源代码
$str = Curl::get($url);
//提取标题内容
preg_match('/
(.*).*发布日期:(.*)  来源.*(.*).*.*转载请注明来源.*.*(.*)转载请注明来源.*/isU', $str, $temp);
//创建pdo对象
$pdo = new PDO('mysql:host=localhost;dbname=lamp;charset=utf8;port=3306','root','');
//发送预处理指令
$stmt = $pdo->prepare('insert into article (title, con, intro, time)values(:title, :con, :intro, :time)');
//参数的绑定
$arr = array(
':title'=>$temp[1],
':con'=>$temp[4],
':intro'=>$temp[3],
':time'=>$temp[2]
);
$stmt->execute($arr);
$id = $pdo->lastInsertId();
return $id;
}
/**
* 采集列表页 获取详情页的url
*/
public function getListUrl($url)
{
//获取当前列表页中的源代码
$data = Curl::get($url);
if(empty($data)){
return false;
}
//正则匹配
preg_match_all('/
.*.*/isU', $data, $temp);
//返回结果
return $temp[1];
}
/**
* 开始采集
*/
public function startCollect(){
$urls = $this->getListUrl('http://www.php1.cn/category/44.html');
//遍历
foreach ($urls as $key => $value) {
$this->getDetailPage($value);
}
}
}
/**
* curl工具类
*/
class Curl{
public static function get($url){
//初始化curl资源
$ch = curl_init($url);
//设置请求选项
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
//发送请求
$res = curl_exec($ch);
//
return $res;
}
}
// $res = get('http://www.php1.cn/Content/XiaoXinGaoXiaoLvDe_sql_ChaXunTaYeHuiDaoZhiWangZhanXiangYingBianMan.html');
//创建对象
// $obj = new PHP1;
// $url = 'http://www.php1.cn/Content/XiaoXinGaoXiaoLvDe_sql_ChaXunTaYeHuiDaoZhiWangZhanXiangYingBianMan.html';
// $res = $obj->getDetailPage($url);
//读取列表页的内容
// $obj->getListUrl('http://www.php1.cn/category/44.html');
// $obj -> startCollect();
//如果代码报错 curl_init undefined
//wampserver->php->php extensions->php_curl
//创建pdo对象
$pdo = new PDO('mysql:host=localhost;dbname=lamp;charset=utf8;port=3306','root','');
//发送预处理指令
$stmt = $pdo->prepare('insert into article (title, con, intro, time)values(:title, :con, :intro, :time)');
//参数的绑定
$arr = array(
':title'=>$temp[1],
':con'=>$temp[4],
':intro'=>$temp[3],
':time'=>$temp[2]
);
$stmt->execute($arr);
$id = $pdo->lastInsertId();
return $id;
}
/**
* 采集列表页 获取详情页的url
*/
public function getListUrl($url)
{
//获取当前列表页中的源代码
$data = Curl::get($url);
if(empty($data)){
return false;
}
//正则匹配
preg_match_all('/
/isU', $data, $temp);
//返回结果
return $temp[1];
}
/**
* 开始采集
*/
public function startCollect(){
$urls = $this->getListUrl('http://www.php1.cn/category/44.html');
//遍历
foreach ($urls as $key => $value) {
$this->getDetailPage($value);
}
}
}
/**
* curl工具类
*/
class Curl{
public static function get($url){
//初始化curl资源
$ch = curl_init($url);
//设置请求选项
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
//发送请求
$res = curl_exec($ch);
//
return $res;
}
}
// $res = get('http://www.php1.cn/Content/XiaoXinGaoXiaoLvDe_sql_ChaXunTaYeHuiDaoZhiWangZhanXiangYingBianMan.html');
//创建对象
// $obj = new PHP1;
// $url = 'http://www.php1.cn/Content/XiaoXinGaoXiaoLvDe_sql_ChaXunTaYeHuiDaoZhiWangZhanXiangYingBianMan.html';
// $res = $obj->getDetailPage($url);
//读取列表页的内容
// $obj->getListUrl('http://www.php1.cn/category/44.html');
// $obj -> startCollect();
//如果代码报错 curl_init undefined
//wampserver->php->php extensions->php_curl
//返回结果
return $temp[1];
}
/**
* 开始采集
*/
public function startCollect(){
$urls = $this->getListUrl('http://www.php1.cn/category/44.html');
//遍历
foreach ($urls as $key => $value) {
$this->getDetailPage($value);
}
}
}
/**
* curl工具类
*/
class Curl{
public static function get($url){
//初始化curl资源
$ch = curl_init($url);
//设置请求选项
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
//发送请求
$res = curl_exec($ch);
//
return $res;
}
}
// $res = get('http://www.php1.cn/Content/XiaoXinGaoXiaoLvDe_sql_ChaXunTaYeHuiDaoZhiWangZhanXiangYingBianMan.html');
//创建对象
// $obj = new PHP1;
// $url = 'http://www.php1.cn/Content/XiaoXinGaoXiaoLvDe_sql_ChaXunTaYeHuiDaoZhiWangZhanXiangYingBianMan.html';
// $res = $obj->getDetailPage($url);
//读取列表页的内容
// $obj->getListUrl('http://www.php1.cn/category/44.html');
// $obj -> startCollect();
//如果代码报错 curl_init undefined
//wampserver->php->php extensions->php_curl

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3 Linux新版
SublimeText3 Linux最新版

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中