from New_MyPython.Community_MyThread.Requse import HttpReq import urllib.parse class GetAllTid: ''' 获取单个用户的所有tid ''' def __init__(self, user): self.HttpRequest = HttpReq.Http() self.tids = [] self.url = 'https://www.oppo.cn/thread/thread/index.json?' ''' 判断用户输入的是uid还是昵称 ''' try: self.uid = int(user) except ValueError: self.name = urllib.parse.quote(user) self.uid = self.select_user_uid() def select_user_uid(self): ''' 搜索用户获取uid ''' url='https://www.oppo.cn/search-member?keyword=%s' % self.name html = self.HttpRequest.get(url) href = 'href="/member-(.*?)-1">' uid = self.HttpRequest.find_data(href, html) return uid[0] def get_user_tids(self): ''' 获取用户所有tid ''' by_url = 'https://www.oppo.cn/thread/thread/index.json?uid=' + self.uid html = self.HttpRequest.get(by_url) totle = '"total":"(.*?)"' tid_nubs = int(self.HttpRequest.find_data(totle, html)[0]) if tid_nubs > 20: # 根据用户动态总数,进行分页 tid_nubs = int(tid_nubs / 20) + 1 else: tid_nubs = 1 for i in range(tid_nubs): by_page_url = by_url + 'page=' + str(i) html = self.HttpRequest.get(by_page_url) _type = ',"tid":"(.*?)"' tids = self.HttpRequest.find_data(_type, html) for tid in tids: if tid != None: self.tids.append(tid) else: print('Tid值为:', tid) return self.tids if __name__ == "__main__": name = 'OPPO' G = GetAllTid(name) # G.select_user_uid() sun = G.get_user_tids() print(sun)