23-1. Web Services. Take the Yahoo! stock quote example (stock.py) and change the application to save the quote data to a file instead of displaying it to the screen.Optional: You may change the script so that users can choose to display the quote data or save it to a file.
1 #!/usr/bin/env python 2 from time import ctime 3 from urllib import urlopen 4 import os 5 6 ticks = ('YHOO', 'GOOG', 'EBAY', 'AMZN') 7 RANGE = range(3, 8) 8 URL = 'http://quote.yahoo.com/d/quotes.csv?s=%s&f=sl1c1p2' 9 TICKS = ('YHOO', 'GOOG', 'EBAY', 'AMZN','AMD') 10 COLS = ('TICKER', 'PRICE', 'CHG', '%AGE') 11 fobj = open('c:/101.txt', 'w') 12 fobj.write('Python-to-stock\r\n\r\n') 13 fobj.write('Prices quoted as of: %s\r\n' % ctime()) 14 for i in range(4): 15 fobj.write('%s\t' % COLS[i]) 16 fobj.write('\n') 17 u = urlopen(URL % ','.join(TICKS)) 18 for data in u: 19 tick, price, chg, per = data.split(',') 20 fobj.write('%s\t' %eval(tick)) 21 fobj.write('%.2f\t' % round(float(price),2)) 22 fobj.write('%s\t' % chg) 23 fobj.write('%s\t\r\n' % eval(per.rstrip())) 24 print 'well done' 25 fobj.close()
23-2. Web Services. Update the Yahoo! stock quote example (stock.py) to download other stock quote data given the additional parameters listed above. Optional: You may add this feature to your solution to the above exercise.
1 #!/usr/bin/env python 2 from time import ctime 3 from urllib import urlopen 4 import os 5 6 ticks = ('YHOO', 'GOOG', 'EBAY', 'AMZN') 7 RANGE = range(3, 8) 8 URL = 'http://quote.yahoo.com/d/quotes.csv?s=%s&f=sl1c1p2oh' 9 TICKS = ('YHOO', 'GOOG', 'EBAY', 'AMZN') 10 COLS = ('TICKER', 'PRICE', 'CHG', '%AGE','LOP','DHP') 11 filename = raw_input('Enter file name: ') 12 fobj = open('c:/102.txt', 'w') 13 fobj.write('Python-to-stock\r\n\r\n') 14 fobj.write('Prices quoted as of: %s\r\n' % ctime()) 15 for i in range(6): 16 fobj.write('%s\t' % COLS[i]) 17 fobj.write('\n') 18 u = urlopen(URL % ','.join(TICKS)) 19 for data in u: 20 tick, price, chg, per, lop, dhp= data.split(',') 21 fobj.write('%s\t' %eval(tick)) 22 fobj.write('%.2f\t' % round(float(price),2)) 23 fobj.write('%s\t' % chg) 24 fobj.write('%s\t' % eval(per.rstrip())) 25 fobj.write('%s\t' % lop)#这里是字符型不是如书上说的numberic 26 fobj.write('%s\t\r\n' % dhp)#这里是字符型不是如书上说的numberic 27 print 'well done' 28 fobj.close()
23-3.
Web Services and the csv Module. Convert stock.py to using the csv module to parse the incoming data, like we did in the
example code snippet. Extra Credit: Do the same thing to the Excel version of this script(estock.py).
1 #!/usr/bin/env python 2 import csv 3 from time import ctime 4 from urllib import urlopen 5 6 ticks = ('YHOO', 'GOOG', 'EBAY', 'AMZN') 7 URL = 'http://quote.yahoo.com/d/quotes.csv?s=%s&f=sl1c1p2' 8 9 print '\nPrices quoted as of:', ctime() 10 print '\nTICKER'.ljust(9), 'PRICE'.ljust(8), 'CHG'.ljust(5), '%AGE' 11 print '------'.ljust(8), '-----'.ljust(8), '---'.ljust(5), '----' 12 u = urlopen(URL % ','.join(ticks)) 13 14 for tick, price, chg, per in csv.reader(u): 15 print tick.ljust(7), ('%.2f' % round(float(price),2)).rjust(6), chg.rjust(6), per.rjust(6) 16 u.close()
23-7.
Microsoft
Office Applications and Web Services. Interface to any existing Web service,whether REST- or URL-based, and write data to an Excel spreadsheet or format thedata nicely into a Word document. Format them properly for printing. Extra Credit:Support both Excel and Word
1 #!/usr/bin/env python 2 from Tkinter import Tk 3 from time import sleep, ctime 4 from tkMessageBox import showwarning 5 from urllib import urlopen 6 import win32com.client as win32 7 8 warn = lambda app: showwarning(app, 'Exit?') 9 RANGE = range(3, 8) 10 TICKS = ('YHOO', 'GOOG', 'EBAY', 'AMZN') 11 COLS = ('TICKER', 'PRICE', 'CHG', '%AGE') 12 URL = 'http://quote.yahoo.com/d/quotes.csv?s=%s&f=sl1c1p2' 13 14 def excel(): 15 app = 'Excel' 16 xl = win32.gencache.EnsureDispatch('%s.Application' % app) 17 ss = xl.Workbooks.Add() 18 sh = ss.ActiveSheet 19 xl.Visible = True 20 sleep(1) 21 22 sh.Cells(1,1).Value = 'Python-to-%s Stock Quote Demo' % app 23 sleep(1) 24 sh.Cells(3, 1).Value = 'Prices quoted as of: %s' % ctime() 25 sleep(1) 26 for i in range(4): 27 sh.Cells(5, i+1).Value = COLS[i] 28 sleep(1) 29 sh.Range(sh.Cells(5, 1), sh.Cells(5, 4)).Font.Bold = True 30 sleep(1) 31 row = 6 32 33 u = urlopen(URL % ','.join(TICKS)) 34 for data in u: 35 tick, price, chg, per = data.split(',') 36 sh.Cells(row, 1).Value = eval(tick) 37 sh.Cells(row, 2).Value = ('%.2f' % round(float(price), 2)) 38 sh.Cells(row, 3).Value = chg 39 sh.Cells(row, 4).Value = eval(per.rstrip()) 40 row += 1 41 sleep(1) 42 f.close() 43 44 warn(app) 45 ss.Close(False) 46 xl.Application.Quit() 47 48 if __name__=='__main__': 49 Tk().withdraw() 50 excel()
1 #!/usr/bin/env python 2 from Tkinter import Tk 3 from time import sleep, ctime 4 from tkMessageBox import showwarning 5 from urllib import urlopen 6 import win32com.client as win32 7 8 warn = lambda app: showwarning(app, 'Exit?') 9 RANGE = range(3, 8) 10 TICKS = ('YHOO', 'GOOG', 'EBAY', 'AMZN') 11 COLS = ('TICKER', 'PRICE', 'CHG', '%AGE') 12 URL = 'http://quote.yahoo.com/d/quotes.csv?s=%s&f=sl1c1p2' 13 14 def word(): 15 app = 'Word' 16 word = win32.gencache.EnsureDispatch('%s.Application' % app) 17 doc = word.Documents.Add() 18 word.Visible = True 19 sleep(1) 20 21 rng = doc.Range(0,0) 22 rng.InsertAfter('Python-to-%s stock\r\n\r\n' % app) 23 sleep(1) 24 rng.InsertAfter('Prices quoted as of: %s\r\n' % ctime()) 25 sleep(1) 26 for i in range(4): 27 rng.InsertAfter('%s\t' % COLS[i]) 28 rng.InsertAfter('\r\n') 29 sleep(1) 30 row=6 31 u = urlopen(URL % ','.join(TICKS)) 32 for data in u: 33 tick, price, chg, per = data.split(',') 34 rng.InsertAfter('%s\t' %eval(tick)) 35 rng.InsertAfter('%.2f\t' % round(float(price),2)) 36 rng.InsertAfter('%s\t' % chg) 37 rng.InsertAfter('%s\t\r\n' % eval(per.rstrip())) 38 sleep(1) 39 f.close() 40 41 warn(app) 42 doc.Close(False) 43 word.Application.Quit() 44 45 if __name__=='__main__': 46 Tk().withdraw() 47 word()