<?php
//在本文件的位置新建一个文件夹来储存文件。文件的编码格式一定要转换,还有获取的时间。
header("content-type:text/html;charset='utf-8'");
set_time_limit(0);
$url="http://sports.sohu.com/nba.shtml";
$str=file_get_contents($url);
$str=mb_convert_encoding($str,"utf-8","GBK");
//echo $str;
$reg='#<div style="HEIGHT: 290px; OVERFLOW: hidden" id="columnID" class="heavyColumn">.*<a onFocus="undefined" href="http://sports.sohu.com/s2016/7265/s460940033/" target="_blank">#isU';
preg_match($reg,$str,$arr);
//var_dump($arr);
$reg2='#<img .* src="(.*)" .*>#isU';
$reg3='#<h4><a.*>(.*)</a></h4>.*<p>(.*)<a.*>.*</a></p>#isU';
preg_match_all($reg2,$arr[0],$data);
preg_match_all($reg3,$arr[0],$data1);
$pdo=new PDO("mysql:host=localhost;dbname=practise","root","root");
$pdo->exec("set names utf8");
//var_dump($data1);die;
unset($data1[0]);
foreach($data1 as $key=>$val)
{
foreach($val as $kk=>$vv)
{
$list[$kk][]=$vv;
}
}
foreach($list as $key=>$val)
{
foreach($val as $kk=>$vv)
{
$li[$key][]="'$vv'";
// $s=$kk+1;
// $sql="insert into demo(title,con) values('$val[$kk]','$val[$s]')";
// $pdo->exec($sql);
//echo $sql;
}
}
foreach($li as $kkk=>$vvv)
{
$ll[]=implode(',',$vvv);
}
foreach($ll as $kkkk=>$vvvv)
{
$sql="insert into demo(title,con) values($vvvv)";
$pdo->exec($sql);
//echo $sql;
}
var_dump($ll);
//var_dump($list);die;
foreach($data[1] as $v)
{
$s=file_get_contents($v);
$ext=substr($v,strrpos($v,"."));
$file="image/".time().rand(1000,9999).$ext;
file_put_contents($file,$s);
}
?>
//在本文件的位置新建一个文件夹来储存文件。文件的编码格式一定要转换,还有获取的时间。
header("content-type:text/html;charset='utf-8'");
set_time_limit(0);
$url="http://sports.sohu.com/nba.shtml";
$str=file_get_contents($url);
$str=mb_convert_encoding($str,"utf-8","GBK");
//echo $str;
$reg='#<div style="HEIGHT: 290px; OVERFLOW: hidden" id="columnID" class="heavyColumn">.*<a onFocus="undefined" href="http://sports.sohu.com/s2016/7265/s460940033/" target="_blank">#isU';
preg_match($reg,$str,$arr);
//var_dump($arr);
$reg2='#<img .* src="(.*)" .*>#isU';
$reg3='#<h4><a.*>(.*)</a></h4>.*<p>(.*)<a.*>.*</a></p>#isU';
preg_match_all($reg2,$arr[0],$data);
preg_match_all($reg3,$arr[0],$data1);
$pdo=new PDO("mysql:host=localhost;dbname=practise","root","root");
$pdo->exec("set names utf8");
//var_dump($data1);die;
unset($data1[0]);
foreach($data1 as $key=>$val)
{
foreach($val as $kk=>$vv)
{
$list[$kk][]=$vv;
}
}
foreach($list as $key=>$val)
{
foreach($val as $kk=>$vv)
{
$li[$key][]="'$vv'";
// $s=$kk+1;
// $sql="insert into demo(title,con) values('$val[$kk]','$val[$s]')";
// $pdo->exec($sql);
//echo $sql;
}
}
foreach($li as $kkk=>$vvv)
{
$ll[]=implode(',',$vvv);
}
foreach($ll as $kkkk=>$vvvv)
{
$sql="insert into demo(title,con) values($vvvv)";
$pdo->exec($sql);
//echo $sql;
}
var_dump($ll);
//var_dump($list);die;
foreach($data[1] as $v)
{
$s=file_get_contents($v);
$ext=substr($v,strrpos($v,"."));
$file="image/".time().rand(1000,9999).$ext;
file_put_contents($file,$s);
}
?>