Skip to content

Instantly share code, notes, and snippets.

@mcdlee
Last active March 10, 2016 07:35
Show Gist options
  • Save mcdlee/52d142d8a0513b4e3d07 to your computer and use it in GitHub Desktop.
Save mcdlee/52d142d8a0513b4e3d07 to your computer and use it in GitHub Desktop.
from lxml import etree
from urllib import request
import csv
import datetime
import os
def getTable():
url = 'http://www.c-bike.com.tw/xml/stationlistopendata.aspx'
response = request.urlopen(url)
xml = response.read()
tree = etree.XML(xml)
db = [["StationID", "StationNO", "name", "available", "empty"]] #list in list
for i in range(0, len(tree[0])):
StationID = tree[0][i].find('StationID').text
StationNO = tree[0][i].find('StationNO').text
name = tree[0][i].find('StationName').text
available = tree[0][i].find('StationNums1').text
empty = tree[0][i].find('StationNums2').text
print(name)
db.append([StationID, StationNO, name, available, empty])
_timestamp = datetime.datetime.now()
year = _timestamp.year
month = _timestamp.month
day = _timestamp.day
time = _timestamp.time().strftime('%H%M%S')
path = 'list/%s/%02d/%02d'%(year, month, day)
filename = '%s/%s.txt'%(path, time)
if not os.path.exists(path):
os.makedirs(path)
with open(filename, "w", newline='') as f:
writer = csv.writer(f)
writer.writerows(db)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment