[python-chinese] 献给买基金的朋友.
Jun Tsai
jun.tsai在gmail.com
星期四 八月 2 15:53:24 HKT 2007
½ñÌìÓÃpythonдµÄÒ»¸ö½Å±¾£¬À´×Ô¶¯×¥È¡½ñÈÕ»ù½ðµÄÊÕÒæÇé¿ö(ÀÁµÃÈ¥ÍøÕ¾¿´),¸Õѧϰpython,´íÎóµÄµØ·½¶àÖ¸Õý.
# -*- coding: UTF-8 -*-
import httplib, urllib,re
import datetime
FUND_CODE = "161706"
SALE_DATE="2007-06-22"
SALE_MONEY = 5000.0
TODAY_DATE=datetime.date.today()
PANEL = "biz.finance.sina.com.cn"
USERAGENT = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1)
Gecko/20060111 Firefox/1.5.0.1'
PATH="/fundinfo/open/lsjz.php?fund_code="
"""
ÓÃÀ´×Ô¶¯×¥È¡»ù½ðµÄÖµ£¬»ñÈ¡¶ÔÓ¦µÄÀûÈóÇé¿ö.
author:jun tsai
revision:$Revision: 3191 $
since:0.1
"""
def get_found_value(fund_code,sale_date,sale_money):
"""×Ô¶¯×¥È¡»ù½ð¾»ÖµµÄ½Å±¾³ÌÐò,ͨ¹ý¸ø¶¨µÄ»ù½ð´úÂ룬Âò»ù½ðµÄÈÕÆÚ£¬ÒÔ¼°Í¶ÈëʹÓõÄÇ®,
À´×Ô¶¯×¥È¡»ù½ðµÄ¾»Öµ£¬ÒÔ¼°ÀûÈó
"""
params = urllib.urlencode
({"startdate1":sale_date,"enddate1":TODAY_DATE})
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain",
'Referer' :'https://'+PANEL+PATH+fund_code,
'User-Agent':USERAGENT
}
conn = httplib.HTTPConnection(PANEL)
conn.request("POST", PATH+fund_code, params, headers)
response = conn.getresponse()
data = response.read()
data=data.decode("gb2312")
conn.close();
pattern = '<title>(.+)\('+fund_code+'\)'
all_matches = re.findall(pattern,data);
fund_name = all_matches[0].encode("utf-8")
# print all_matches[0]
pattern='<a
href=\'./lsjz_dwjz.php\?jzrq=(.*)\'[\s]+target=_blank>(.*)</a>'
all_matches=re.findall(pattern,data);
today_value = float(all_matches[0][1])
sale_value = float(all_matches[len(all_matches)-1][1])
sale_count = sale_money/sale_value
value=(today_value-sale_value)*sale_count
print
"|"+construct_block(10,fund_code)+"|"+construct_head_block(20,fund_name)+"|"
+construct_block(10,sale_value.__str__())+"|"
+construct_block(20,sale_count.__str__())+"|"
+construct_block(10,today_value.__str__())+"|"
+construct_block(20,value.__str__())+"|"
def construct_block(length,str):
r=' '+str
while(length>len(r)):
r+=' '
return r
def construct_head_block(length,str):
r=' '+str
head_str_len=len(str.decode("utf-8"))
while(length>(len(r)-head_str_len)):
r+=' '
return r
print
"+-----------------------------------------------------------------------------------------------+"
print
"|"+construct_head_block(10,"´úÂë")+"|"+construct_head_block(20,"Ãû³Æ")+"|"+construct_head_block(10,"¹ºÂò¾»Öµ")+"|"+construct_head_block(20,"¹ºÂòÊý")+"|"+construct_head_block(10,"½ñÈÕ¾»Öµ")+"|"+construct_head_block(20,"ÀûÈó")+"|"
print
"+-----------------------------------------------------------------------------------------------+"
get_found_value("161706","2007-06-22",5000.0)
get_found_value("260110","2007-06-10",5000.0)
get_found_value("070011","2007-06-23",5000.0)
print
"+-----------------------------------------------------------------------------------------------+"
--
Welcome to China Java Users Group(CNJUG).
http://cnjug.dev.java.net
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070802/b54de990/attachment.html
关于邮件列表 python-chinese 的更多信息