python selenium+requests+browsermobproxy 捕获网络请求,模拟提交刷课

仅以此文记录python selenium+requests+browsermobproxy 捕获网络请求,模拟提交刷课


from selenium.webdriver import Chrome, ActionChains
from selenium.webdriver import ChromeOptions
import os

#pip install browsermob-proxy
#https://github.com/lightbody/browsermob-proxy/releases
from browsermobproxy import Server
from selenium.webdriver.chrome.options import Options

from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time, re, urllib.request,requests,json

class gzwy(object):
    def __init__(self):
        option = ChromeOptions()
        option.add_experimental_option("excludeSwitches", ['enable-automation']) #不显示自动化控制
        #option.add_argument(r'--user-data-dir=C:\Users\ASUS\AppData\Local\Google\Chrome\User Data\Default')# 设置个人资料路径

        #设置代理
        server = Server(r'data\browsermob-proxy-2.1.4\bin\browsermob-proxy.bat')
        server.start()
        self.proxy = server.create_proxy()
        option.add_argument('--proxy-server={0}'.format(self.proxy.proxy))
        # 解决 您的连接不是私密连接问题
        option.add_argument('--ignore-certificate-errors')
        option.add_argument('--ignore-urlfetcher-cert-requests')

        self.proxy.new_har("test", options={'captureContent': True, 'captureHeaders': True})#开启一个代理监控

        s = Service('data/Application/chromedriver.exe')
        self.driver = Chrome(service=s,options=option)
        self.driver.implicitly_wait(10)
        self.driver.set_window_size(1000, 1000)

    def login(self,username,password):

        self.driver.get('https://****.gov.cn/dsfa/nc/pc/logins/views/login.html')

        usename_input = self.driver.find_element('xpath',  '//*[@id="username"]')  # 账号
        usename_input.send_keys(username)
        time.sleep(0.1)
        password_input = self.driver.find_element('xpath', '//*[@id="password"]')  # 密码
        password_input.send_keys(password)
        time.sleep(0.1)

        xieyi_link = self.driver.find_element('xpath','//*[@id="vertifyCode"]') #验证码
        xieyi_link.send_keys('')

        button = self.driver.find_element('xpath', '//*[@id="LoginBtn"]')
        button.click()  # 点击重置
        time.sleep(0.1)
        self.driver.get('https://****.gov.cn/dsfa/nc/pc/main/views/main.html')
        self.driver.implicitly_wait(10)
    def gundongtiao(self):
        target = self.driver.find_element('xpath','/html/body/div[4]')
        self.driver.execute_script("arguments[0].scrollIntoView();", target)  # 将滚动条拖动到元素可见的地方
        time.sleep(0.2)
    def zhongwangyuan(self):
        time.sleep(1.5)
        self.gundongtiao()
        #点击进入网院
        zwyimg = self.driver.find_element('xpath','/html/body/div[4]/ul/li[2]/i/img')
        zwyimg.click()
        self.driver.implicitly_wait(10)
        all_handles = self.driver.window_handles  # 保存窗口句柄
        for i in all_handles:
            self.driver.switch_to.window(i)
        time.sleep(5)
        #进入专题
        self.driver.get('https://cela.*******.com/dsfa/nc/ztzl/ztzxkc/views/xisijuan.html?cela_sso_logged=true')
        self.driver.implicitly_wait(5)
        time.sleep(5)
        self.zhuantiliebiao()

    def zhuantiliebiao(self):
        zhuantilist = [
            'https://cela.*******.com/dsfa/nc/pc/course/views/course.html?type=2&id=3a56eadcd52640d9b08da950efd8baa3',
            'https://cela.*******.com/dsfa/nc/pc/course/views/course.html?type=2&id=880bb592a97e4fd6961bcd3753e68764',
            'https://cela.*******.com/dsfa/nc/pc/course/views/course.html?type=2&id=e9c8366c8524434e939c296bf99fd47a',
            'https://cela.*******.com/dsfa/nc/pc/course/views/course.html?type=2&id=4e2b4adb38704926a8960aff8b7cc531',

            'https://cela.*******.com/dsfa/nc/pc/course/views/course.html?type=2&id=559702aa3e1149c7b2219f8f60a10d03',
            'https://cela.*******.com/dsfa/nc/pc/course/views/course.html?type=2&id=c110cbf10a38416cba3402082bc8a5e5',
        ]
        all_handles = self.driver.window_handles  # 保存窗口句柄
        newhandle = self.driver.switch_to.new_window()#打开新窗口

        for url in zhuantilist:
            self.driver.get(url)
            time.sleep(1)
            self.driver.refresh()#刷新
            self.driver.implicitly_wait(5)
            relid = url.split('=')[-1]
            dwid,sfwc,wcjd,kcid,kjid,id,plwdid,NC_XXGL_XXJL_LOG_id,kjsc = self.get_sfwcAnd_wcjd(relid)
            print(dwid, sfwc, wcjd, kcid, kjid, id, plwdid, NC_XXGL_XXJL_LOG_id, kjsc)
                #relid
            print(sfwc)
            print(wcjd)

            #self.driver.find_element('xpath','//*[@id="chapterWarp"]/div/div/ul/li').click()#点击左边视频列表第一个视频
            time.sleep(10)

            #怎么学
            self.driver.find_element('xpath',
                                     '/html/body/div[1]/div[2]/div/div[1]/div/div[2]/div[2]/div[1]/button').click()  # 播放视频
            time.sleep(3)
            time.sleep(11)
            if (sfwc != '1'):
                self.study(relid)
            # while(sfwc!='1'):
            #     self.study()
                #self.stdy(dwid,kcid,kjsc,relid)
                #time.sleep(1000)
    def get_sfwcAnd_wcjd(self,relid):
        result = self.proxy.har
        for entry in result['log']['entries']:
            _url = entry['request']['url']
            # print(_url)
            # # 根据URL找到数据接口
            if "kjk/frontuse/getKcDetailMenu?type=2&relid="+relid in _url:
                print(_url)
                # 获取接口请求内容
                _request = entry['request']
                # 获取接口返回内容
                _response = entry['response']
                print(_response)
                content = json.loads(_response['content']['text'])
                print(content)
                dwid = content['dwid']
                sfwc = content['data']['dyInfo'][0]['kcParaInfo'][0]['sfwc']
                wcjd = content['data']['dyInfo'][0]['kcParaInfo'][0]['wcjd']
                kcid = content['data']['dyInfo'][0]['kcParaInfo'][0]['kjParaInfo'][0]['kcid']
                kjid = content['data']['dyInfo'][0]['kcParaInfo'][0]['kjParaInfo'][0]['kjid']
                id = content['data']['dyInfo'][0]['kcParaInfo'][0]['kjParaInfo'][0]['id']
                plwdid = content['data']['dyInfo'][0]['kcParaInfo'][0]['kjParaInfo'][0]['id']
                NC_XXGL_XXJL_LOG_id = content['data']['dyInfo'][0]['kcParaInfo'][0]['kjParaInfo'][0]['NC_XXGL_XXJL_LOG_id']
                kjsc = content['data']['dyInfo'][0]['kcParaInfo'][0]['kjParaInfo'][0]['kjsc']
                return dwid,sfwc,wcjd,kcid,kjid,id,plwdid,NC_XXGL_XXJL_LOG_id,kjsc
    def stdy(self,relid,courseid,kjsc):
        url = 'https://cela.*******.com/dsfa/nc/xxgl/xxjl/addXxjlByKczj'
        result = self.proxy.har
        for entry in result['log']['entries']:
            _url = entry['request']['url']
            # print(_url)
            # # 根据URL找到数据接口
            if "kjk/frontuse/getKcDetailMenu?type=2&relid="+relid in _url:
                print(_url)
                # 获取接口请求内容
                _request = entry['request']
                # 获取接口返回内容
                _response = entry['response']
                # 构造headers
                headers = {}
                for value in _request['headers']:
                    headers[value['name']] = value['value']
                print('构造headers:',headers)
                break


        bigtimespan = str(kjsc).split(':')
        totaltime = float(bigtimespan[0]) * 3600 + float(bigtimespan[1]) * 60 + float(bigtimespan[2])
        print(totaltime)

        timespan = '00:00:00'.split(':')
        timespan_h = int(timespan[0])
        timespan_m = int(timespan[1])
        timespan_s = int(timespan[2])

        postData ={
                "relid": relid,
                "bigtimespan": "00:24:40",
                "timespan": timespan,
                "pass": "0",
                "sumtime": "0.0",
                "everytime": "10",
                "courseid": courseid
            }
        for i in range(0, int(totaltime) + 50, 10):
            # 更新timespan
            timespan_s = timespan_s + 10
            if (timespan_s >= 60):
                timespan_m = timespan_m + 1
                timespan_s = timespan_s % 60
            if (timespan_m >= 60):
                timespan_h = timespan_h + 1
                timespan_m = timespan_m % 60

            newtimespan = str("{:0>2d}".format(timespan_h)) + ':' + str(
                "{:0>2d}".format(timespan_m)) + ':' + str("{:0>2d}".format(timespan_s))
            postData['timespan'] = newtimespan

            # postData['sumtime'] = float(postData['sumtime']) + 10
            print('postData:', postData)
            # 这里模拟提交
            # driver.execute_script(js)提交
            r = requests.post(url,headers=headers, data=postData)
            print('模拟提交返回数据:', r.text)

    def study(self,relid):
        result = self.proxy.har
        for entry in result['log']['entries']:
            _url = entry['request']['url']
            #print(_url)
            # # 根据URL找到数据接口
            if "xxjl/addXxjlByKczj" in _url:
                print(_url)
                # 获取接口请求内容
                _request = entry['request']
                # 获取接口返回内容
                _response = entry['response']
                print(_request)

                #构造模拟提交数据
                url = _request['url']
                # 构造headers
                headers = {}
                for value in _request['headers']:
                    headers[value['name']] = value['value']
                print('构造headers:',headers)
                postData = {}
                for value in _request['postData']['params']:
                    postData[value['name']] = value['value']
                print('postData:', postData)
                print('relid,postData[relid] :',relid,postData['relid'] )

                bigtimespan = str(postData['bigtimespan']).split(':')
                sumtime = float(postData['sumtime'])
                print(bigtimespan)
                totaltime = float(bigtimespan[0]) * 3600 + float(bigtimespan[1]) * 60 + float(bigtimespan[2])
                print(totaltime)
                timespan = str(postData['timespan']).split(':')
                print('timespan:', timespan)

                timespan_h = int(timespan[0])
                timespan_m = int(timespan[1])
                timespan_s = int(timespan[2])

                for i in range(int(sumtime), int(totaltime)+50, 10):
                    # 更新timespan
                    timespan_s = timespan_s + 10
                    if (timespan_s >= 60):
                        timespan_m = timespan_m + 1
                        timespan_s = timespan_s % 60
                    if (timespan_m >= 60):
                        timespan_h = timespan_h + 1
                        timespan_m = timespan_m % 60

                    newtimespan = str("{:0>2d}".format(timespan_h)) + ':' + str(
                        "{:0>2d}".format(timespan_m)) + ':' + str("{:0>2d}".format(timespan_s))
                    postData['timespan'] = newtimespan
                    #postData['sumtime'] = float(postData['sumtime']) + 10
                    print('postData:', postData)
                    #这里模拟提交
                    #driver.execute_script(js)提交
                    r = requests.post(url,headers=headers,data=postData)
                    print('模拟提交返回数据:',r.text)





if __name__ == '__main__':
    gzwy = gzwy()
    gzwy.login('202156875','1388888888')
    gzwy.zhongwangyuan()

侵删

** 仅以此文记录代码,请勿用作非法用途,否则后果由使用者自负,如有侵权请联系删除。 **

你可能感兴趣的:(Python,python,selenium,chrome)