my-request练习

本文介绍了一个Python脚本,用于实现华为内部论坛的自动化登录、获取特定日期的帖子URL、读取帖子楼层信息及发布评论。脚本使用requests库进行HTTP请求,BeautifulSoup进行HTML解析,并记录关键操作的时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值