//电影链接地址
$url = "http://www.dy2018.com/i/96514.html";
//获取网页内容
$str = file_get_contents($url);
if($str){
//把 HTML 实体转换为字符
$to_strs = html_entity_decode($str, ENT_QUOTES);
//使用函数去掉特殊字符
function DeleteHtml($str)
{
$str = trim($str); //清除字符串两边的空格
$str = preg_replace("/\t/","",$str); //使用正则表达式替换内容,如:空格,换行,并将替换为空。
$str = preg_replace("/\r\n/","",$str);
$str = preg_replace("/\r/","",$str);
$str = preg_replace("/\n/","",$str);
$str = preg_replace("/ /","",$str);
$str = preg_replace("/ /","",$str); //匹配html中的空格
return trim($str); //返回字符串
}
//使用str_replace 来替换换行
$strs = DeleteHtml($to_strs);
//设置空数组
$match = array();
//用正则匹配所需要的数据
preg_match_all('/<divclass="title_all"><h1>(.*?)<\/h1><\/div>/', $strs, $title);
preg_match_all('/<strongclass="rank">(.*?)<\/strong>/', $strs, $score);
preg_match_all('/<spanclass="updatetime">(.*?)<\/span>/', $strs, $time);
preg_match_all('/<tdstyle="WORD-WRAP:break-word"bgcolor="#fdfddf"><ahref="(.*?)">(.*?)<\/a><\/td>/', $strs, $d_url1);
//将匹配的数据放大数组中
$match['title'] = $title[1][0];
$match['score'] = $score[1][0];
$match['time'] = $time[1][0];
$match['d_url1'] = $d_url1[1][0];
$match['d_url2'] = $d_url1[1][1];
$match['d_url3'] = $d_url1[1][2];
//连接数据库并判断
$link = mysql_connect("localhost","root","root")or die("数据库连接失败!");
//选择数据库
mysql_select_db("mycat",$link);
//设置字符集
mysql_query('set names gb2312');
//判断获取数据类型方式
if(!empty($match['d_url2']) || !empty($match['d_url3'])){
$sql = "insert into movie (title, score, c_time,d_url1,d_url2,d_url3) values ('$match[title]','$match[score]','$match[time]','$match[d_url1]','$match[d_url2]','$match[d_url3]')";
}else{
$sql = "insert into movie (title, score, c_time,d_url1) values ('$match[title]','$match[score]','$match[time]','$match[d_url1]')";
}
//执行mysql语句操作
$result = mysql_query($sql);
//关闭数据库
mysql_close($link);
}else{
echo '超出服务器响应时间!';
}
$url = "http://www.dy2018.com/i/96514.html";
//获取网页内容
$str = file_get_contents($url);
if($str){
//把 HTML 实体转换为字符
$to_strs = html_entity_decode($str, ENT_QUOTES);
//使用函数去掉特殊字符
function DeleteHtml($str)
{
$str = trim($str); //清除字符串两边的空格
$str = preg_replace("/\t/","",$str); //使用正则表达式替换内容,如:空格,换行,并将替换为空。
$str = preg_replace("/\r\n/","",$str);
$str = preg_replace("/\r/","",$str);
$str = preg_replace("/\n/","",$str);
$str = preg_replace("/ /","",$str);
$str = preg_replace("/ /","",$str); //匹配html中的空格
return trim($str); //返回字符串
}
//使用str_replace 来替换换行
$strs = DeleteHtml($to_strs);
//设置空数组
$match = array();
//用正则匹配所需要的数据
preg_match_all('/<divclass="title_all"><h1>(.*?)<\/h1><\/div>/', $strs, $title);
preg_match_all('/<strongclass="rank">(.*?)<\/strong>/', $strs, $score);
preg_match_all('/<spanclass="updatetime">(.*?)<\/span>/', $strs, $time);
preg_match_all('/<tdstyle="WORD-WRAP:break-word"bgcolor="#fdfddf"><ahref="(.*?)">(.*?)<\/a><\/td>/', $strs, $d_url1);
//将匹配的数据放大数组中
$match['title'] = $title[1][0];
$match['score'] = $score[1][0];
$match['time'] = $time[1][0];
$match['d_url1'] = $d_url1[1][0];
$match['d_url2'] = $d_url1[1][1];
$match['d_url3'] = $d_url1[1][2];
//连接数据库并判断
$link = mysql_connect("localhost","root","root")or die("数据库连接失败!");
//选择数据库
mysql_select_db("mycat",$link);
//设置字符集
mysql_query('set names gb2312');
//判断获取数据类型方式
if(!empty($match['d_url2']) || !empty($match['d_url3'])){
$sql = "insert into movie (title, score, c_time,d_url1,d_url2,d_url3) values ('$match[title]','$match[score]','$match[time]','$match[d_url1]','$match[d_url2]','$match[d_url3]')";
}else{
$sql = "insert into movie (title, score, c_time,d_url1) values ('$match[title]','$match[score]','$match[time]','$match[d_url1]')";
}
//执行mysql语句操作
$result = mysql_query($sql);
//关闭数据库
mysql_close($link);
}else{
echo '超出服务器响应时间!';
}