A simple collection of Yahoo's data _php skills

Source: Internet
Author: User
Before in the company has written something similar, this time to help the previous boss wrote a simple acquisition program.

It's very simple. Sweat. There is no technical content.
Data Source: http://cn.finance.yahoo.co ...
Demo Address: Http://traffic02.100steps .....

(modified, added the data caching function.) Khan, did not use Lite_cache, himself wrote a simple kind of. )
Copy Code code as follows:

?
Set_time_limit (0);

$max _time=3600;
$cache _file= ' cache_yahoo.txt ';

$nowtime =time ();
if (!file_exists ($cache _file)) {
$filetime = 0;
}else{
$filetime =filemtime ($cache _file);
}

if ($filetime + $max _time< $nowtime) {
Update
Ob_start ();
function Get_yahoo_info ($url) {
$content =file_get_contents ($url);

$pattern = ' |<tr class= ' Yfnc_modtitle1 ' ><td><small><b> (. *) </b> \ ((. *) \) (. *) </ SMALL&GT;&LT;/TD&GT;&LT;TD align= "right" >| U ';
Preg_match_all ($pattern, $content, $out);
$info [' company_name ']= $out [1][0];
$info [' Company_stock ']= $out [2][0];

$pattern = ' |<td class= "Yfnc_tablehead1" width= "52%" > Recent transaction Price: &LT;/TD&GT;&LT;TD class= "Yfnc_tabledata1" >< Big><b> (. *) </b></big></td>| U ';
Preg_match_all ($pattern, $content, $out);
$info [' Stock_price ']= $out [1][0];

$pattern = ' |<td class= "Yfnc_tablehead1" width= "52%" > Ups and Downs: </td><td class= "Yfnc_tabledata1" > (. *) < /td>| U ';
Preg_match_all ($pattern, $content, $out);
$info [' Stock_upordown ']= $out [1][0];

$pattern = ' |<td class= "Yfnc_tablehead1" width= "54%" > Market value: </td><td class= "Yfnc_tabledata1" > (. *) < /td>| U ';
Preg_match_all ($pattern, $content, $out);
$info [' Stock_value ']= $out [1][0];

return $info;
}
$urls =explode (', ', file_get_contents (' url.txt '));
$i = 0;
$matchs =array ();
foreach ($urls as $url) {
$rs =get_yahoo_info ($url);
if (!empty ($rs)) {
$matchs [$i]= $rs;
$i + +;
$rs = ';
}
}

Print_r (Get_yahoo_info (' Http://cn.finance.yahoo.com/q?s=000063.SZ '));
?>
<table border= "1" width= "100%" cellpadding= "6" style= "Border-collapse:collapse" bordercolor= "#8CBFF9" cellspacing = "0" bgcolor= "#DFEDFD" >
<tr>
&LT;TD width= "height=" "valign=" Top "bordercolor=" #ABD0FA "bgcolor=" #E1EEFD "> </td>
&LT;TD valign= "Top" bordercolor= "#ABD0FA" bgcolor= "#E1EEFD" ><div align= "center" > Company name </div></td >
&LT;TD valign= "Top" bordercolor= "#ABD0FA" bgcolor= "#E1EEFD" ><div align= "center" > Stock Code &LT;/DIV&GT;&LT;/TD >
&LT;TD valign= "Top" bordercolor= "#ABD0FA" bgcolor= "#E1EEFD" ><div align= "center" > Stock price &LT;/DIV&GT;&LT;/TD >
&LT;TD valign= "Top" bordercolor= "#ABD0FA" bgcolor= "#E1EEFD" ><div align= "center" > Stock gain &LT;/DIV&GT;&LT;/TD >
&LT;TD valign= "Top" bordercolor= "#ABD0FA" bgcolor= "#E1EEFD" ><div align= "Center" > Market Value </div></td>
</tr>
?
$i = 1;
foreach ($matchs as $match) {
?>
<tr>
&LT;TD height= "" align= "center" valign= "Top" bordercolor= "#ABD0FA" bgcolor= "#FFFFFF" ><?= $i?></td>
&LT;TD valign= "Top" bordercolor= "#ABD0FA" bgcolor= "#FFFFFF" ><div align= "center" ><?= $match [' Company_ Name ']?></div></td>
&LT;TD valign= "Top" bordercolor= "#ABD0FA" bgcolor= "#FFFFFF" ><div align= "center" ><?= $match [' Company_ Stock ']?></div></td>
&LT;TD valign= "Top" bordercolor= "#ABD0FA" bgcolor= "#FFFFFF" ><div align= "center" ><?= $match [' Stock_ Price ']?></div></td>
&LT;TD valign= "Top" bordercolor= "#ABD0FA" bgcolor= "#FFFFFF" ><div align= "center" ><?= $match [' Stock_ Upordown ']?></div></td>
&LT;TD valign= "Top" bordercolor= "#ABD0FA" bgcolor= "#FFFFFF" ><div align= "center" ><?= $match [' Stock_ Value ']?></div></td>
</tr>
?
$i + +;
}?>
</table>
?
$content =ob_get_clean ();
$FP =fopen ($cache _file, ' W ');
if (! $fp) echo ' Open file failed ';
if (Flock ($FP, LOCK_EX)) {//Exclusive lock
if (!fwrite ($FP, $content)) echo ' File write Failed ';
Flock ($FP, lock_un); Release lock
} else {
echo "couldn ' t lock the file!";
if (!fwrite ($FP, $content)) echo ' File write Failed ';
}
Fclose ($FP);
Echo ' Cache time: '. Time ();
Echo $content;
}else{
Echo ' cache! ';
Echo file_get_contents ($cache _file);
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.