Requirement:
Need to scrap the data of crypto currency details for given date ranges. Run time we are passing dates as parameters to the python script. Data is available in Tabular Format.
Code to execute:
# parameters in yyyymmdd format
>>>python beautifulsoup.py 20160101 20171104
Sample Output:
#file name: beautifulsoup.py
from bs4 import BeautifulSoup
import sys
import requests
from datetime import date
now = date.today()
#read run time parameters
startdt = sys.argv[1]
enddt = sys.argv[2]
#Website details
url = 'coinmarketcap.com/currencies/bitcoin/historical-data/?start='+startdt+'&end='+enddt+''
r = requests.get("https://" +url)
data = r.text
soup = BeautifulSoup(data,"lxml")
table = soup.find("table", { "class" : "table" })
currencytype = soup.find("select",{"class" : "pointer"})
row=table.findAll('tr')
row1 = table.findAll('li')
records = []
count = 0
#code to write data to text file.
def writetext():
with open('c:\output_%s.txt'%now, 'w') as f:
f.write("Date".ljust(20,' ')+
"|Open".ljust(21,' ')+
"|High".ljust(21,' ')+
"|Low".ljust(21,' ')+
"|Close".ljust(21,' ')+
"|Volume".ljust(21,' ')+
"|MarketCap".ljust(20,' ')+
"\n")
for row in table.findAll("tr")[1:]:
cells = row.findAll("td")
Date = cells[0].find(text=True)
Open = cells[1].find(text=True)
High = cells[2].find(text=True)
Low = cells[3].find(text=True)
Close = cells[4].find(text=True)
Volume = cells[5].find(text=True)
MarketCap = cells[6].find(text=True)
record = (Date,Open,High,Low,Close,Volume,MarketCap)
f.write("%s|%s|%s|%s|%s|%s|%s \n" % \
(Date.ljust(20,' '),
Open.ljust(20,' '),
High.ljust(20,' '),
Low.ljust(20,' '),
Close.ljust(20,' '),
Volume.ljust(20,' '),
MarketCap.ljust(20,' ')
)
)
#calling function
writetext();
Need to scrap the data of crypto currency details for given date ranges. Run time we are passing dates as parameters to the python script. Data is available in Tabular Format.
Code to execute:
# parameters in yyyymmdd format
>>>python beautifulsoup.py 20160101 20171104
Sample Output:
----------------------------------------------------------------------------------------------------------------
Code:
#file name: beautifulsoup.py
from bs4 import BeautifulSoup
import sys
import requests
from datetime import date
now = date.today()
#read run time parameters
startdt = sys.argv[1]
enddt = sys.argv[2]
#Website details
url = 'coinmarketcap.com/currencies/bitcoin/historical-data/?start='+startdt+'&end='+enddt+''
r = requests.get("https://" +url)
data = r.text
soup = BeautifulSoup(data,"lxml")
table = soup.find("table", { "class" : "table" })
currencytype = soup.find("select",{"class" : "pointer"})
row=table.findAll('tr')
row1 = table.findAll('li')
records = []
count = 0
#code to write data to text file.
def writetext():
with open('c:\output_%s.txt'%now, 'w') as f:
f.write("Date".ljust(20,' ')+
"|Open".ljust(21,' ')+
"|High".ljust(21,' ')+
"|Low".ljust(21,' ')+
"|Close".ljust(21,' ')+
"|Volume".ljust(21,' ')+
"|MarketCap".ljust(20,' ')+
"\n")
for row in table.findAll("tr")[1:]:
cells = row.findAll("td")
Date = cells[0].find(text=True)
Open = cells[1].find(text=True)
High = cells[2].find(text=True)
Low = cells[3].find(text=True)
Close = cells[4].find(text=True)
Volume = cells[5].find(text=True)
MarketCap = cells[6].find(text=True)
record = (Date,Open,High,Low,Close,Volume,MarketCap)
f.write("%s|%s|%s|%s|%s|%s|%s \n" % \
(Date.ljust(20,' '),
Open.ljust(20,' '),
High.ljust(20,' '),
Low.ljust(20,' '),
Close.ljust(20,' '),
Volume.ljust(20,' '),
MarketCap.ljust(20,' ')
)
)
#calling function
writetext();
Looks Good, Helpful
ReplyDelete