my-request練習

#coding:utf-8

import requests,time
from bs4 import BeautifulSoup
from log_time import log_time

class request_w3():
    @log_time
    def __init__(self):
        self.session = requests.Session()

    # login W3
    def login_W3(self, ID, passwd):
        url = "http://login.huawei.com/login/login.do"
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Cache-Control': 'max-age=0',
            'Connection': 'keep-alive',
            'Content-Length': '180',
            'Content-Type': 'application/x-www-form-urlencoded'
        }
        data = {
            "actionFlag": "loginAuthenticate",
            "lang": "en",
            "loginMethod": "login",
            "loginPageType": "mix",
            "uid":ID,
            "password": passwd,
            "verifyCode": "2345"

        }
        r = self.session.post(url, headers=self.headers, data=data)
        if r.status_code == 200:
            print("login W3 success")

    def getday(self):
        year = str(time.strftime("%Y", time.localtime()))
        month = str(int(time.strftime("%m", time.localtime())))
        day = str(int(time.strftime("%d", time.localtime())))
        print("finding URL")
        month1 = str(time.strftime("%m", time.localtime()))
        day1 = str(time.strftime("%d", time.localtime()))
        today = year + "/" + month + "/" + day
        today1 = year + "/" + month1 + "/" + day
        today2 = year + "/" + month1 + "/" + day1
        get_day = []
        get_day.append(today)
        get_day.append(today1)
        get_day.append(today2)
        return get_day

    @log_time
    def get_URL(self):
        url_category = "http://3ms.huawei.com/hi/index.php?app=group&mod=Forum&act=indexList&gid=8271&1=1&" \
                       "category=1205689&t=0.37333295193817007&t_type=forum&category=1205689"
        URL = ""
        getURL = self.session.get(url_category)
        txt = getURL.text
        soup = BeautifulSoup(txt, 'lxml')
        match = soup.findAll('a')
        a_list = list(match)
        for today in self.getday():
            for l in a_list:
                if today in str(l):
                    URL = l.attrs['href']
                    print("find URL success",l.attrs['href'])
                    return URL
                else:
                    continue
            if URL == "":
                print("find URL fail")
                return URL

    def get_mapId(self, URL):
        self.mapId = URL.split("mapId=", 1)[1].split("&")[0]
        print("mapId is ", self.mapId)
        return self.mapId

    def get_tid(self, URL):
        self.tid = URL.split("thread_", 1)[1].split(".html")[0]
        print("tid is ", self.tid)
        return self.tid

    @log_time
    def get_postURL(self):
        self.post_URL = "http://3ms.huawei.com/hi/index.php?app=group&mod=Forum&act=post&gid=8271&mapId="+self.mapId
        return self.post_URL

    @log_time
    def get_floor(self, URL):
        floorlist = []
        txt = self.session.get(URL).text
        soup = BeautifulSoup(txt, 'lxml')
        match = soup.findAll('span', class_="R f12 cl06")
        for m in match:
            floorlist.append(str(m))
        print("now is %s floor " % len(floorlist))
        return len(floorlist)

    @log_time
    def post_comment(self, comment):
        data1 = {'tid': self.tid,
                 'gid': '8271',
                 'content': comment,
                 '__hi3ms__[156471679272]': '6f4f86470991711a64bfbd61c455d2f4'
                 }
        re = self.session.post(self.post_URL, headers=self.headers, data=data1)
        if re.status_code == 200:
            print("post comment success")
        else:
            print("post error , error code is ",re.status_code)


if __name__=="__main__":
    movie = request_w3()
    movie.login_W3("***","***")
    while True:
        URL = movie.get_URL()
        if URL:
            break
    # URL = "http://3ms.huawei.com/hi/group/8271/thread_7824234.html?mapId=9617194&for_statistic_from=all_group_forum"
    mapId = movie.get_mapId(URL)
    tid = movie.get_tid(URL)
    post_URL = movie.get_postURL()
    while True:
        floor = movie.get_floor(URL)
        if floor == 37:
            print("ready")
            break
    movie.post_comment("test")
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章