Python抓劵商公司,忘了附上lib在使用的函數,很抱歉,在此補上缺少的部分。
#劵商總公司
def Broker():
while True:
url = "http://www.twse.com.tw/ch/products/broker_service/broker_list.php"
webcode = urllib.urlopen(url)
if webcode.code == 200:
stock = Broker_list()
stock.parse(webcode.read())
webcode.close()
stock.close()
if len(stock.brokerid) > 0 :
return stock
else:
return 0
class Broker_list(SGMLParser):
def __init__(self):
SGMLParser.__init__(self)
def reset(self):
SGMLParser.reset(self)
self.broker_id_open = False
self.brokerid = []
self.brokeropen = []
self.gray12count = 0
self.broker_name = False
self.brokername = []
self.brokernamecount = 0
self.gray12count2 = 0
self.broker_address_tel = False
self.brokeraddress = []
self.brokertel = []
self.tilcount = 0
def parse(self,data):
self.feed(data)
self.close()
def start_a(self,attrs):
for name, value in attrs:
if name == 'class' and value == 'basictxt':
self.broker_name = True
self.brokernamecount += 1
def start_td(self, attrs):
for name, value in attrs:
#print attrs
if name == 'class':
if value == 'til_2':
self.tilcount += 1
if self.tilcount == 6 and value == 'gray12':
if len(attrs) == 1:
self.broker_id_open = True
self.gray12count += 1
if len(attrs) == 2:
self.broker_address_tel = True
self.gray12count2 += 1
def handle_data(self, text):
if self.broker_id_open:
if self.gray12count == 1:
self.brokerid.append(text)
self.broker_id_open = False
#print "broker_id : " + text
elif self.gray12count == 2:
self.brokeropen.append(text)
self.broker_id_open = False
self.gray12count = 0
#print "broker_open : " + text
if self.broker_name:
if self.brokernamecount == 1:
#print "1 : " + text
self.brokername.append(text)
self.broker_name = False
elif self.brokernamecount == 2:
#print "2 : " + text
self.broker_name = False
self.brokernamecount = 0
if self.broker_address_tel:
if self.gray12count2 == 1:
#print "address : " + text
self.broker_address_tel = False
self.brokeraddress.append(text)
elif self.gray12count2 == 2:
#print "tel : " + text
self.broker_address_tel = False
self.brokertel.append(text)
self.gray12count2 = 0
#劵商分公司
def Broker2():
while True:
url = "http://www.twse.com.tw/ch/products/broker_service/broker2_list.php"
brokerkind = "slave"
webcode = urllib.urlopen(url)
if webcode.code == 200:
stock = Broker2_list()
stock.parse(webcode.read())
webcode.close()
stock.close()
if len(stock.brokerid) > 0 :
return stock
else:
return 0
class Broker2_list(SGMLParser):
def __init__(self):
SGMLParser.__init__(self)
def reset(self):
SGMLParser.reset(self)
self.bPrintDetail = False
self.broker_id_open = False
self.brokerid = []
self.brokeropen = []
self.gray12count = 0
self.broker_name = False
self.brokername = []
self.brokernamecount = 0
self.gray12count2 = 0
self.broker_address_tel = False
self.brokeraddress = []
self.brokertel = []
self.tilcount = 0
def parse(self,data):
self.feed(data)
self.close()
def start_a(self,attrs):
for name, value in attrs:
if name == 'class' and value == 'basictxt':
self.broker_name = True
self.brokernamecount += 1
def start_td(self, attrs):
for name, value in attrs:
#print attrs
if name == 'class':
if value == 'til_2':
self.tilcount += 1
if self.tilcount == 5 and value == 'gray12':
if len(attrs) == 1:
self.broker_id_open = True
self.gray12count += 1
if len(attrs) == 2:
self.broker_address_tel = True
self.gray12count2 += 1
def handle_data(self, text):
if self.broker_id_open:
if self.gray12count == 1:
self.brokerid.append(text)
self.broker_id_open = False
#print "broker_id : " + text
elif self.gray12count == 2:
self.brokeropen.append(text)
self.broker_id_open = False
self.gray12count = 0
#print "broker_open : " + text
if self.broker_name:
if self.brokernamecount == 1:
data = text.replace(' ', '')
#print "1 : " + data
self.brokername.append(data)
self.broker_name = False
self.brokernamecount = 0
if self.broker_address_tel:
if self.gray12count2 == 1:
#print "address : " + text
self.broker_address_tel = False
self.brokeraddress.append(text)
elif self.gray12count2 == 2:
#print "tel : " + text
self.broker_address_tel = False
self.brokertel.append(text)
self.gray12count2 = 0