I actually write a code, which one i am scraping a table from a website, and import to an CSV file. My question is, how can i make a new columns and put the actually date in every cells in this columns (while there are data).
Here is my code:
import urllib2
from bs4 import BeautifulSoup
import unicodecsv as csv
import os
import sys
import io
import time
import datetime
filename=r'output.csv'
resultcsv=open(filename,"wb")
output=csv.writer(resultcsv, delimiter=';',quotechar = '"', quoting=csv.QUOTE_NONNUMERIC, encoding='latin-1')
header = ['Pénznem', 'Devizanév','Egység','Pénznem_Forintban', 'Dátum']
output.writerow(header)
def make_soup(url):
thepage = urllib2.urlopen(url)
soupdata = BeautifulSoup(thepage, "html.parser")
return soupdata
def to_2d(l,n):
return [l[i:i+n] for i in range(0, len(l), n)]
soup=make_soup("https://www.mnb.hu/arfolyamok")
datatable=[]
for record in soup.findAll('tr'):
for data in record.findAll('td'):
datatable.append(data.text)
maindatatable = to_2d(datatable, 4)
output.writerows(maindatatable)
resultcsv.close()
print maindatatable
Where i have to put and what i have to change in my date code, to get the correct result?
now = time.strftime('%d-%m-%Y')
ido = to_2d(now, 5)
output.writerows(ido)