python读取大盘指数

#coding=utf-8

import HTMLParser
import time
import urllib2

# 继承语法
class MyParser(HTMLParser.HTMLParser): 
    targetid = ''   
     
    def __init__(self,targetid):
        HTMLParser.HTMLParser.__init__(self) 
        self.targetid = targetid
               
    def handle_starttag(self, tag, attrs):
        self.is_h4 = ""
        # 这里重新定义了处理开始标签的函数
        if tag == 'span':
            # 判断标签<a>的属性
            for name,value in attrs:
                if name == 'id':
                    if value == self.targetid:
                        self.is_h4 = 1
                    
    def handle_endtag(self, tag):
        self.is_h4 = -1
                            
    def handle_data(self, data):
        if self.is_h4 == 1:
            print "大盘指数:", data
            
            
           
st = time.time()   
pageContent =  urllib2.urlopen('https://www.google.com.hk/finance?client=ob&hl=zh-CN&q=SHA:000001').read()                 
my = MyParser('ref_7521596_l')
my.feed(pageContent)

end = time.time()
print '所花时间: %d 秒'%(end-st)
 

你可能感兴趣的:(python读取大盘指数)