forked from je-suis-tm/web-scraping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
US Treasury.py
58 lines (34 loc) · 1.23 KB
/
US Treasury.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: filetype=python
# In[1]:
#this script scrapes us treasury website
#for constant maturity treasury rate
#the treasury rate will be used as risk free interest rate
#which is crucial to the vix calculator
# https://github.com/je-suis-tm/quant-trading/blob/master/VIX%20Calculator.py
import pandas as pd
import requests
import os
os.chdir('d:/')
# In[2]:
#scraping function
def scrape(url):
session=requests.Session()
session.headers.update(
{'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'})
response=session.get(url,verify=False)
return response
# In[3]:
def main():
url='https://www.treasury.gov/resource-center/data-chart-center/interest-rates/Pages/TextView.aspx?data=yield'
response=scrape(url)
#the second table is yield curve rate
data=pd.read_html(response.text)[1]
#cleanse
data=data.melt(id_vars='Date',var_name='maturity')
#convert to datetime
data['Date']=pd.to_datetime(data['Date'])
data.to_csv('treasury yield curve rates.csv',index=False)
if __name__ == "__main__":
main()