例子,php获取新浪星座与运势。
<?php
function html2txt($document){
$search = array('@<script[^>]*?>.*?</script>@si', // Strip out javascript
'@<[/!]*?[^<>]*?>@si', // Strip out HTML tags
'@<style[^>]*?>.*?</style>@siU', // Strip style tags properly
'@<![sS]*?--[ tnr]*>@', // Strip multi-line comments including CDATA
'@s+| @'
);
$text = preg_replace($search, '', $document);
return $text;
} www.jb200.com
function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://astro.sina.com.cn/fate/astro_aries.html?prourl=0');//伪造来源
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/'.mt_rand(2, 9).'.0 (Windows NT 6.1; WOW64; rv:'.mt_rand(10, 30).'.0) Gecko/'.mt_rand(2000, 2014).'0101 Firefox/'.mt_rand(10, 30).'.0');//随机header
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
foreach(array($constellation) as $astro){
preg_match('/<span>(.*?)<em>(.*?)</em></span>.*?有效日期:(.*?)综合运势(.*?)爱情运势(.*?)工作状况(.*?)理财投资(.*?)健康指数(.*?)商谈指数(.*?)幸运颜色(.*?)幸运数字(.*?)速配星座(.*?)<div class="lotconts">(.*?)</div>/isu', curl('http://vip.astro.sina.com.cn/astro/view/'.$astro.'/day/'), $matches);
file_put_contents($astro, json_encode(array($matches[1], $matches[2], html2txt($matches[3]), substr_count($matches[4], '<img'), substr_count($matches[5], '<img'), substr_count($matches[6], '<img'), substr_count($matches[7], '<img'), html2txt($matches[8]), html2txt($matches[9]), html2txt($matches[10]), html2txt($matches[11]), html2txt($matches[12]), $matches[13])));
unset($matches);
$array=json_decode(file_get_contents($astro),true);
}
//明天的日期
$mingtian=date('Ymd',strtotime('+1 day'));
foreach(array($constellation) as $astro){
preg_match('/<span>(.*?)<em>(.*?)</em></span>.*?有效日期:(.*?)综合运势(.*?)爱情运势(.*?)工作状况(.*?)理财投资(.*?)健康指数(.*?)商谈指数(.*?)幸运颜色(.*?)幸运数字(.*?)速配星座(.*?)<div class="lotconts">(.*?)</div>/isu', curl('http://vip.astro.sina.com.cn/astro/view/'.$astro.'/day/'.$mingtian.''), $matches);
//这样正则,可以更小的受新浪改版所带来的影响
file_put_contents($astro, json_encode(array($matches[1], $matches[2], html2txt($matches[3]), substr_count($matches[4], '<img'), substr_count($matches[5], '<img'), substr_count($matches[6], '<img'), substr_count($matches[7], '<img'), html2txt($matches[8]), html2txt($matches[9]), html2txt($matches[10]), html2txt($matches[11]), html2txt($matches[12]), $matches[13])));
unset($matches);
$array2=json_decode(file_get_contents($astro),true);
}
//今天的运势
print_r($array)
//明天的运势
print_r($array2)
?>