例子,php获取新浪星座与运势。
function html2txt($document){ - $search = array('@@si', // Strip out javascript
- '@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags
- '@@siU', // Strip style tags properly
- '@@', // Strip multi-line comments including CDATA
- '@\s+| @'
- );
- $text = preg_replace($search, '', $document);
- return $text;
- } bbs.it-home.org
- 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('/(.*?)(.*?)<\/em><\/span>.*?有效日期:(.*?)综合运势(.*?)爱情运势(.*?)工作状况(.*?)理财投资(.*?)健康指数(.*?)商谈指数(.*?)幸运颜色(.*?)幸运数字(.*?)速配星座(.*?)(.*?)<\/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], 'unset($matches);
- $array=json_decode(file_get_contents($astro),true);
- }
-
- //明天的日期
- $mingtian=date('Ymd',strtotime('+1 day'));
- foreach(array($constellation) as $astro){
- preg_match('/(.*?)(.*?)<\/em><\/span>.*?有效日期:(.*?)综合运势(.*?)爱情运势(.*?)工作状况(.*?)理财投资(.*?)健康指数(.*?)商谈指数(.*?)幸运颜色(.*?)幸运数字(.*?)速配星座(.*?)(.*?)<\/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], 'unset($matches); - $array2=json_decode(file_get_contents($astro),true);
- }
-
- //今天的运势
- print_r($array)
- //明天的运势
- print_r($array2)
- ?>
|