I'm trying to convert the table in the following site to an xls table:
http://www.dekel.co.il/madad-lazarchan
The following is the code I came up with from researching:
from bs4 import BeautifulSoup
import pandas as pd
from urllib2 import urlopen
import requests
import csv
url='http://www.dekel.co.il/madad-lazarchan'
table = pd.read_html(requests.get(url).text, attrs={"class" : "medadimborder"})
print table</code>
How can I get it to display the headers properly and output to a csv or xls file?
If I add the following:
table.to_csv('test.csv')
instead of the print row I get this error:
'list' object has no attribute 'to_csv'
Thanks in Advance!
Okay based on the comments maybe I shouldn't use panda or read_html as I want a table and not a list. I wrote the following code but now the printout has delimiters and looks like I lost the header row. Also still not sure how to export it to csv file.
from bs4 import BeautifulSoup
import urllib2
import csv
soup = BeautifulSoup(urllib2.urlopen('http://www.dekel.co.il/madad-lazarchan').read(), 'html')
data = []
table = soup.find("table", attrs={"class" : "medadimborder"})
table_body = table.find('tbody')
rows = table_body.findAll('tr')
for row in rows:
cols = row.findAll('td')
cols = [ele.text.strip() for ele in cols]
print cols
[u'01/16', u'130.7915', u'122.4640', u'117.9807', u'112.2557', u'105.8017', u'100.5720', u'98.6'] [u'12/15', u'131.4547', u'123.0850', u'118.5790', u'112.8249', u'106.3383', u'101.0820', u'99.1'] [u'11/15', u'131.5874', u'123.2092', u'118.6986', u'112.9387', u'106.4456', u'101.1840', u'99.2']