-
Notifications
You must be signed in to change notification settings - Fork 5
/
EPS_3_OTS.py
executable file
·87 lines (61 loc) · 2.85 KB
/
EPS_3_OTS.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- coding: utf-8 -*-
import urllib
from bs4 import BeautifulSoup
import os
from datetime import datetime
import time
import csv
from time import strftime
year = strftime('%Y')
start = time.time()
content = "贏要衝,輸要縮." #沒有辦法換行
time_now = datetime.now().strftime("%Y%m%d_%H%M%S") #今天的日期 ex:2015-0411
title = str(time_now+"__EPS__OTS") #Email郵件的標題 ex:2015-0411-選股機器人
realtime_txt = open("OTS.txt", 'w') #開啟檔案,w沒有該檔案就新增
year = str(int(year)-1911)
realtime_txt.write(year+"年第三季EPS_OTS"+"\n")
realtime_txt.write("代號 "+" 本季EPS 去年同期EPS 今年EPS累積 去年同期EPS累積 值利率"+"\n")
f = open('/home/tim/GMAIL.txt','r') #於前一個相對目錄中放置登入GMAIL帳號密碼,目的為了不再GitHub顯示出來.
ID = f.readline().strip('\n') #不包含換行符號\n
PW = f.readline().strip('\n')
with open('OTS.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',') #以模組csv裡的函數reader來讀取csvfile變數,區隔符號為逗號(,),讀取後存到readCSV變數裡
for row in readCSV: #就readCSV裡的所有資料(以列為單位)
print row[1]
url = 'http://mops.twse.com.tw/mops/web/ajax_t164sb04?'\
'encodeURIComponent=1&step=1&firstin=1&off=1&keyword4=&code1=&TYPEK2=&checkbtn=&queryName=co_id&TYPEK=all&isnew=false&co_id='+row[0]+'&year='+year+'&season=03'
try:
time.sleep(10)
response = urllib.urlopen(url)
html = response.read()
sp = BeautifulSoup(html.decode('utf-8')) #cp950
#print(sp)
print url
trs=sp.find_all('tr')
for tr in trs:
tds=tr.find_all('td')
for td in tds:
if (td.get_text().strip()==u"基本每股盈餘") :
if (tds[1].get_text().strip()!=''):
print(row[0],year,'3',tds[1].get_text().strip())
print(row[0],year,'3',tds[3].get_text().strip())
print(row[0],year,'3',tds[5].get_text().strip())
print(row[0],year,'3',tds[7].get_text().strip())
if (tds[1].get_text().strip()!=''):
realtime_txt.write(row[0]+" "+tds[1].get_text().strip()+" "+tds[3].get_text().strip()+" "+tds[5].get_text().strip()+" "+tds[7].get_text().strip()+" "+row[4]+"\n")
except:
pass
end = time.time()
elapsed = end - start
print( "Time taken: ", elapsed, "seconds.")
realtime_txt.write("Time taken: "+ str(elapsed) + " seconds.")
realtime_txt.close()
os.system('sendEmail -o \
-f u160895@taipower.com.tw \
-t "WEI <weihautin@gmail.com>" \
-s smtp.gmail.com:587 \
-xu %s \
-xp %s \
-u %s \
-o message-file=/home/tim/OTS.txt \
'%(ID, PW, title))