code_junkie

Saving HTML tables to a Database

Nov 10th, 2011
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. from BeautifulSoup import BeautifulSoup
  2. import urllib,string,csv,sys,os
  3. from string import replace
  4.  
  5. date_s = '&date1=01/01/08'
  6. date_f = '&date=11/10/08'
  7. fx_url = 'http://www.oanda.com/convert/fxhistory?date_fmt=us'
  8. fx_url_end = '&lang=en&margin_fixed=0&format=CSV&redirected=1'
  9. cur1,cur2 = 'USD','AUD'
  10. fx_url = fx_url + date_f + date_s + '&exch=' + cur1 +'&exch2=' + cur1
  11. fx_url = fx_url +'&expr=' + cur2 + '&expr2=' + cur2 + fx_url_end
  12. data = urllib.urlopen(fx_url).read()
  13. soup = BeautifulSoup(data)
  14. data = str(soup.findAll('pre', limit=1))
  15. data = replace(data,'[<pre>','')
  16. data = replace(data,'</pre>]','')
  17. file_location = '/Users/location_edit_this'
  18. file_name = file_location + 'usd_aus.csv'
  19. file = open(file_name,"w")
  20. file.write(data)
  21. file.close()
Add Comment
Please, Sign In to add comment