|
例子,php获取新浪星座与运势。
-
-
function html2txt($document){
- $search = array('@<script>]*?>.*?</script>@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('/(.*?)(.*?).*?有效日期:(.*?)综合运势(.*?)爱情运势(.*?)工作状况(.*?)理财投资(.*?)健康指数(.*?)商谈指数(.*?)幸运颜色(.*?)幸运数字(.*?)速配星座(.*?)
(.*?)/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('/(.*?)(.*?).*?有效日期:(.*?)综合运势(.*?)爱情运势(.*?)工作状况(.*?)理财投资(.*?)健康指数(.*?)商谈指数(.*?)幸运颜色(.*?)幸运数字(.*?)速配星座(.*?)
(.*?)/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)
- ?>
-
复制代码
|