from bs4 import BeautifulSoup
import urllib2
# import csv

# url = 'http://www.data.jma.go.jp/obd/stats/etrn/view/monthly_s3_en.php?block_no=47401&view=1'
url="http://ka1/go/eidogo/tourney/table.html"
html = urllib2.urlopen(url).read()
soup = BeautifulSoup(html)
table = soup.select_one("table.tourney-games")
# python3 just use th.text
headers = [th.text.encode("utf-8") for th in table.select("tr th")]

# with open("table.csv", "w") as f:
#     wr = csv.writer(f)
#     wr.writerow(headers)
#     wr.writerows([[td.text.encode("utf-8") for td in row.find_all("td")] for row in table.select("tr + tr")])
# table = soup.select_one("table.data2_s")
# headers = [th.text.encode("utf-8") for th in table.select("tr th")]

with open("out.csv", "w") as f:
    f.write(",".join(headers) + "\n")
    f.writelines(",".join([td.text.encode("utf-8") for td in row.find_all("td")]) + "\n" 
                 for row in table.select("tr + tr"))
