易班第二弹

def get_vote_url():
    for i in range(50):
        start_url = "http://www.yiban.cn/Newgroup/showMorePub/group_id/457740/puid/7245623/type/3/page/{0}".format(i)
        resp = person.get(url=start_url)
        response = Selector(resp)
        url_list = response.css(' .vote-itd2 a::attr(href)').extract()
        next_url = response.css('.pager .next::attr(href)').extract()
        print(next_url)  # 下一页url
        print(url_list)  # 整页投票url
        for i in url_list:
            vote_id = re.findall("vote_id/(.*?)/puid", i)
            author_id = re.findall("actor_id/(.*?)/status", i)
            # print(author_id)
            # print(vote_id)
            # print(type(vote_id))
            # vote_url = "http://www.yiban.cn{0}".format(i)
            # "pagetype": 2,  (1代表未过期,2代表已过期)
            # 提取网页过期时间, 与当前时间做对比
            # vote / vote / showDetail / vote_id / 54280789 / puid / 7245623 / group_id / 457740 / actor_id / 12806206 / status / 1
            vate_data = {
                "vote_id": vote_id[0],
                "uid": 12806146,
                "puid": 7245623,
                "pagetype": 1,
                "group_id": 457740,
                "actor_id": 12806146,
                "top_power": "f",
                "edit_power": "f",
                "end_power": "f",
                "del_power": "f",
                "block_power": "f",
                "isSchoolVerify": 1,
                "is_public": "f",
                "is_anonymous": "t",
                "token": "",
                "out_power": "f",
                "isMember": "",
                "url%5BgetVoteDetail%5D": "vote%2Fvote%2FgetVoteDetail",
                "url%5Boutput%5D": "%2Fvote%2FExpand%2Foutput",
                "rl%5BgetCommentDetail%5D": "vote%2Fvote%2FgetCommentDetail",
                "url%5BaddComment%5D": "vote%2Fvote%2FaddComment",
                "url%5BeditLove%5D": "vote%2Fvote%2FeditLove",
                "url%5Bvote%5D": "vote%2Fvote%2Fact",
                "url%5BsetIsTop%5D": "vote%2FExpand%2FsetIsTop",
                "url%5BsetManualEndVote%5D": "vote%2FExpand%2FsetManualEndVote",
                "url%5BdelVote%5D": "vote%2FExpand%2FdelVote",
                "url%5BdelComment%5D": "vote%2Fvote%2FdelComment",
                "url%5BshieldVote%5D": "vote%2FExpand%2FshieldVote",
                "url%5BgetAnonymous%5D": "vote%2FExpand%2FgetAnonymous",
                "url%5BuserInfo%5D": "user%2Findex%2Findex",
                "isLogin": 1,
                "isOrganization": 0,
                "ispublic": 0,
            }
            vote_res = person.post(url="http://www.yiban.cn/vote/vote/getVoteDetail", data=vate_data, cookies=cookies,
                                   headers={
                                       'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36',
                                       'Accept': 'application/json, text/javascript, */*; q=0.01',
                                       'Accept-Language': 'zh-CN,zh;q=0.9'})
            response_f = vote_res.json()
            # print(response_f)
            # print(type(response_f))
            dict1 = response_f['data']
            dict_l = dict1['vote_list']
            dict_t = dict1['option_list']
            # print(dict_t)

            # 选取全部voptions_id 投票选项id
            v_list = []
            for i in dict_t:
                v_list.append(dict(i)["id"])
            print(v_list)

            # 提取是否已投票
            v_list1 = []
            for m in dict_t:
                try:
                    v_list1.append(dict(m)["is_vote"])
                except KeyError:
                    pass
            print(v_list1)

            # min
            minimum = int(dict_l['minimum'])  # 投票数 单选or多选
            scopeMax = int(dict_l['scopeMax'])

            # mount_id
            mount_id = int(dict_l['mount_id'])  # 投票数 单选or多选

            # end_time
            end_time = dict_l['voteValue'] + ":00"  # 结束时间

            # 时间转时间戳
            timeArray = time.strptime(end_time, "%Y-%m-%d %H:%M:%S")  # 时间数组
            timeStamp = int(time.mktime(timeArray))  # 时间戳
            print(timeStamp)
            if cmp_time(int(timeStamp)) == 1 and len(v_list1) == 0:  # 判断是否过期或已投票
                voptions_id = voptions_id_chiose(minimum, v_list)
                vate_choise(vote_id[0], author_id[0], minimum, scopeMax, voptions_id)  # 投票
                vate_comment(vote_id, author_id, mount_id)  # 评论
            else:
                vate_comment(vote_id, author_id, mount_id)


# def vote_chenge(next_url):
#     if next_url:
#         next_url ="{0}next_url".format("www.yiban.cn/")
#     else:
#         brack
#     ret


# 判断多选or单选
def voptions_id_chiose(minimum, v_list):
    import random
    if minimum == 1:
        voptions_id = []
        i = random.randint(0, minimum)
        voptions_id.append(v_list[i])
        voptions_id1 = [str(i) for i in voptions_id]  # 遍历各元素使之成为字符元素
        str_voptions_id = "".join(voptions_id1)
        # 单选
    else:
        n = len(v_list)
        m = random.sample(range(0, n), minimum)
        voptions_id = []
        for i in m:
            voptions_id.append(v_list[i])
        print(voptions_id)
        voptions_id1 = [str(i) for i in voptions_id]  # # 遍历各元素使之成为字符元素
        str_voptions_id = ",".join(voptions_id1)
        # 多选
    return str_voptions_id


# 判断投票是否过期
def cmp_time(end_time):
    n_time = time.time()
    pagetype = 1
    if int(n_time) > end_time:
        pagetype = 2
    return pagetype


# 选择投票
def vate_choise(vote_id, author_id, minimum, scopeMax, voptions_id):
    vate_data = {
        "puid": 7245623,
        "group_id": 457740,
        "vote_id": vote_id,
        "actor_id": author_id,
        "voptions_id": voptions_id,
        "minimum": minimum,
        "scopeMax": scopeMax,
    }
    res = person.post(url="http://www.yiban.cn/vote/vote/act", data=vate_data, cookies=cookies,
                      headers={
                          'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36',
                          'Accept': 'application/json, text/javascript, */*; q=0.01',
                          'Accept-Language': 'zh-CN,zh;q=0.9'})
    print(res.json())


# 投票评论
def vate_comment(vote_id, author_id, mount_id):
    comment_data = {
        "mountid": mount_id,
        "msg": "完美",
        "group_id": 457740,
        "actor_id": 12806146,
        "vote_id": vote_id,
        "author_id": author_id,
        "puid": 7245623,
        "reply_comment_id": 0,
        "reply_user_id": 0,
    }
    res = person.post(url="http://www.yiban.cn/vote/vote/addComment", data=comment_data, cookies=cookies,
                      headers={
                          'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36',
                          'Accept': 'application/json, text/javascript, */*; q=0.01',
                          'Accept-Language': 'zh-CN,zh;q=0.9'})
    print(res.json())

猜你喜欢

转载自www.cnblogs.com/jum-bolg/p/10902242.html