#_*_coding: utf-8 _*_
#__author__ = 'Alihanniba'

import urllib.request
# from urllib.request import urlopen
import urllib.error
import re
import _thread
import time

class BDTB:
    def __init__(self, bashUrl, seeLZ):
        self.bashUrl = bashUrl
        self.seeLZ = '?see_lz='+str(seeLZ)

    def getPage(self, pageNum):
        try:
            url = self.bashUrl + self.seeLZ + '&pn=' + str(pageNum)
            request = urllib.request.Request(url)
            response = urllib.request.urlopen(request)
            content = response.read().decode('UTF-8')
            print(content)
            return response
        except urllib.error.URLError as e:
            if hasattr(e, 'code'):
                print(u"连接百度贴吧失败,失败原因: ", e.code)
            if hasattr(e, 'reason'):
                print(e.reason)
    def getTitle(self):
        page = self.getPage(1)
        pattern = re.compile('<h3 class="core_title_txt.*?>(.*?)</h3>', re.S)
        result = re.search(pattern, str(page))
        if result:
            print(result.group())
        else:
            print('----没有匹配到数据-----')
            return None
            


bashUrl = 'http://tieba.baidu.com/p/3138733512'
bdtb = BDTB(bashUrl, 1)
# bdtb.getPage(1)
bdtb.getTitle()
