[Python] The Shanghai/Shanghai

Source: Internet
Author: User
Tags timedelta

[Python] The Shanghai/Shanghai

Shanghai and Shenzhen dragon and tiger lists data further processed to calculate the rise and fall in the next five days

Advance data:

Previously processed csv file

Add "[wait]" before file name to wait for program Processing

Python code obtains data from Yahoo stock history data api, and calculates the increase or decrease in the next five days

Yahoo data api format:

PriceUrl = 'HTTP: // table.finance.yahoo.com/table.csv? Comment'
# % :000001. sz
# END: % 2: month-1% 3: day % 4: Year
# STRAT: % 5: month-1% 6: day % 7: Year

Advance data:

After calculation, use the excel condition format and save it as an excel file:

Code:

1 # coding = UTF-8 2 3 # Read the csv file starting with '[wait]' 4 # copyright @ WangXinsheng 5 # http://www.cnblogs.com/wangxinsheng/ 6 import OS 7 import gzip 8 import re 9 import http. cookiejar 10 import urllib. request 11 import urllib. parse 12 import time 13 import datetime 14 15 def getOpener (head): 16 # deal with the Cookies 17 cj = http. cookiejar. cookieJar () 18 pro = urllib. request. HTTPCookieProcessor (cj) 19 opener = Urllib. request. build_opener (pro) 20 header = [] 21 for key, value in head. items (): 22 elem = (key, value) 23 header. append (elem) 24 opener. addheaders = header 25 return opener 26 27 def ungzip (data): 28 try: # try to decompress 29 print ('unzipping ..... ') 30 data = gzip. decompress (data) 31 print ('decompressed! ') 32 bytes T: 33 print ('uncompress, do not decompress') 34 return data 35 36 # constant 37 header = {38 # 'connection': 'Keep-alive ', 39 'accept': '*/*', 40' Accept-Language ': 'zh-CN, zh; q = 66661', 41 'user-agent ': 'mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/6666', 42 'Accept-encoding': 'gzip, deflate ', 43 'host': 'yahoo. com ', 44 'Referer': 'http: // www.yahoo.com' 45} 46 p RiceUrl = 'HTTP: // table.finance.yahoo.com/table.csv? \ 47 s ={% 1} & d ={% 2} & e ={% 3} \ 48 then '49 # % 1:20.01. sz 50 # END: % 2: month-1% 3: Day-1% 4: year 51 # STRAT: % 5: month-1% 6: day % 7: year 52 53 54 path = R '. '55 files = OS. listdir (path) 56 files. sort () 57 out = [] 58 59 for f in files: 60 if (f. startswith ('[wait]') and 61 f.endswith('.csv '): 62 # Read File 63 print ('read file:' + path + '/' + f) 64 65 f = open (path + '/' + f, 'rt ') 66 infos = f. readlines () 67 f. Close () 68 69 I = 0 70 add = False 71 for info in infos: 72 if (I = 0): 73 I = I + 1 74 info = info. replace ('\ n', '') +'," One day later "," Two days later "," three days later "," Four days later ", "Five days later" \ n' 75 out. append (info) 76 continue 77 elif (len (info. split (',')> 9): 78 out. append (info) 79 continue 80 else: 81 # confirm the data range to be retrieved 82 tmp = info. split (',') 83 try: 84 timeArray = time. strptime (tmp [0], "% Y-% m-% d") 85 bytes T: 86 timeArray = time. strptime (tmp [0], "% Y/% m/% d ") 87 timeStamp = int (time. mktime (timeArray) 88 fromDay = datetime. datetime. utcfromtimestamp (timeStamp) 89 fromDay = fromDay + datetime. timedelta (days = 1) 90 endDay = fromDay + datetime. timedelta (days = 15) 91 code = tmp [1]. replace ('"',''). replace ("'", "") 92 if (code. startswith ('6'): 93 code = code + '. ss '94 else: 95 code = code + '. sz '96 url = priceUrl. replace ('{% 1}', code ). replace ('{% 2 }', Str (endDay. month-1) 97 url = url. replace ('{% 3}', str (endDay. day )). replace ('{% 4}', str (endDay. year) 98 url = url. replace ('{% 5}', str (fromDay. month-1 )). replace ('{% 6}', str (fromDay. day) 99 url = url. replace ('{% 7}', str (fromDay. year) 100 print ('capture URL: '+ url) 101 102 # Get price via Yahoo 103 dd = ''104 try: 105 opener = getOpener (header) 106 op = opener. open (url) 107 data = op. read () 108 data = ungzip (data) 109 dd = data. Decode () 110 failed T: 111 print ('network capture failed') 112 out. append (info) 113 continue114 # calculate the percentage of increase/decrease by 115 if (dd! = ''): 116 dataInfo = dd. split ('\ n') 117 j = 0118 dayCount = 0119 startPrice = 0120 for x in range (len (dataInfo)-,-1 ): 121 # process data 122 if (dataInfo [x] = ''): 123 continue124 # print (dataInfo [x]) 125 if (dayCount> 5 ): 126 break127 di = dataInfo [x] 128 if (dayCount = 0): 129 startPrice = float (di. split (',') [4]) 130 elif (int (di. split (',') [5])! = 0): 131 add = True132 closeP = float (di. split (',') [4]) 133 info = info. replace ('\ n', '') +'," '+ str (round (closeP-startPrice)/startPrice * 100,2 )) + '% [' + str (closeP) + '] "'134 # print (info) 135 if (dayCount = 0 or int (di. split (',') [5])! = 0): 136 dayCount = dayCount + 1137 138 if (add): 139 out. append (info + '\ n') 140 # print (out) 141 continue142 # output 143 ff = open (path +'/'+ f. name. replace ('[wait]', '[Processed]'), 'w') 144 for o in out: 145 ff. write (o) 146 ff. close () 147 print ('processed \ n file address: '+ path +'/'+ f. name. replace ('[wait]', '[Processed]') 148 else: 149 continue

 

Related Article

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.