python 抓取腾讯微博数据并做简单的分析

来源:互联网 发布:nodejs高级编程 中文 编辑:程序博客网 时间:2024/06/13 10:29

利用python去读取腾讯微博中某个人的数据,统计和他相关的一些连接,并对每个打印每个连接的数据,本人采用的python版本为3.3.

from html.parser import HTMLParser  import urllib.requestimport repattern = re.compile(r'http://t.qq.com/*?')class MyParser(HTMLParser):        a_txt =False    total  = 0    sample_list = []    def inputSelfAddress(self, name):        self.sample_list.append(name)    def __init__(self):       HTMLParser.__init__(self)                   def handle_starttag(self, tag, attrs):        if tag=='div':            for name,value in attrs:               if (name == 'class')and(value == 'msgCnt'):                     self.a_txt=True        if tag=='a':            for name, value in attrs:                if(name == 'href'):                     match = pattern.match(value)                     if(match):                         if value in  self.sample_list:                             return                         else:                            self.total=self.total+1                            self.sample_list.append(value)                def handle_data(self, data):         if self.a_txt:              print (data)             self.a_txt=False headers = ('User-Agent','Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11')opener = urllib.request.build_opener()opener.addheaders = [headers]strAddress = "http://t.qq.com/xiaoRainie"fp =opener.open(strAddress)mybytes = fp.read()mystr = mybytes.decode("utf8")fp.close()myparser = MyParser()myparser.inputSelfAddress(strAddress)myparser.feed(mystr) print("总数:", myparser.total-11)for i in range(int(myparser.total), 1, -1):    print(myparser.sample_list[i-1])print("结束")for i in range(int(myparser.total), 1, -1):    fp =opener.open(myparser.sample_list[i-1])    myparser.inputSelfAddress(myparser.sample_list[i-1])    mybytes = fp.read()    mystr = mybytes.decode("utf8")    fp.close()    myparser = MyParser()      myparser.feed(mystr)    

这里简单使用了python 的html 分析模块,和url 处理模块!

原创粉丝点击