oppo社区,学习笔记(二)

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值