def loginin(self):
     """
         功能:登录托福网站
         参数:None
         返回:ok:登陆成功,error:登录失败       
     """
     self.tuoful_session.cookies.clear()
     response = self.tuoful_session.get("http://toefl.etest.net.cn/cn/")
     text = response.content.decode("gb2312")
     # print(text)
     # if "登录" not in text:
     #     return "error"
     cook = response.cookies
     print(cook)
     coo = {c.name: c.value for c in response.cookies}
     print(coo)
     if "yunsuo_session_verify" in coo:
         return "error"
     else:
         temp_list = []
         for key,value in coo.items():
             srt = "%s=%s"%(key,value)
             temp_list.append(srt)
         self.Cookie_str = ";".join(temp_list)
     header_yz_login = headers_all(self.Cookie_str)["header_yz_login"]
     picUrl = "https://toefl.etest.net.cn/cn/"+ str(int(time.time()*1000))+str(random.random())[1:]+"VerifyCode3.jpg"
     content_pic = self.tuoful_session.get(picUrl,headers = header_yz_login)
     num = only_num()
     #获取验证码内容
     codeContent = self.jpg2str(num, content_pic)
     headers_login = headers_all(self.Cookie_str)["headers_login"]
     s = str_md5("%s%s"%(self.neea_pwd, self.neea))
     x = s+codeContent
     pwd = str_md5(x)
     payload = {"username":"******"%self.neea,"__act":"__id.24.TOEFLAPP.appadp.actLogin","password":pwd,"LoginCode":codeContent,"btn_submit.x":"0","btn_submit.y":"0"}
     v = self.tuoful_session.post("https://toefl.etest.net.cn/cn/TOEFLAPP",data=payload, headers=headers_login)
     text = v.content.decode("gb2312")
     if "0; URL=/cn/MyHome/?" in text:
         return "ok"
                 
     elif "验证码错误" in text:
         now = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
         print("%slogin验证码错误"%(now))
         return "error"
     elif "密码不正确" in text:
         now = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
         print("%s密码不正确或验证码获取有误"%(now))
         return "error"
     else:
         print("发生未知错误 请查看login_error.txt文件")
         with open("login_error.txt", "w") as f:
             f.write(text)
         return "error"
 def admintable(self):
     headers_admintable = headers_all(self.Cookie_str)["headers_admintable"]
     response_query = self.tuoful_session.get("https://toefl.etest.net.cn/cn/CityAdminTable",headers = headers_admintable)
     text = response_query.content.decode("gb2312")
     if "发生错误" in text:
         return "Access violation"
     #如果系统忙,休息1min后再次测试
     while self.checkBusy(text):
         print('系统繁忙,35s后再次测试')
         time.sleep(35)
         r = self.tuoful_session.get("https://toefl.etest.net.cn/cn/CityAdminTable",headers = headers_admintable)
         text = str(r.content)
     #判断是否进入正常考试查询页面
     if text.find('省份') == -1:
         print('进入选择页面失败!')
         if "please relogin first" in text:
             print("please relogin first")
             return "please relogin first"
         elif "注册并付费" in text:
             return "ok"
         else:
             print("发生未知错误 请查看information.html文件")
             with open("information.html","w") as f:
                 f.write(text)
             return "error"
     #获取验证码地址
     pattern1 = '<img src="/cn/'
     pattern2 = 'VerifyCode2.jpg'
     pos1 = text.find(pattern1)
     pos2 = text.find(pattern2)
     picUrl = 'https://toefl.etest.net.cn/'+text[pos1+11:pos2]+'VerifyCode2.jpg'
     num = only_num()
     content_pic = self.tuoful_session.get(picUrl) #获取验证码内容
     codeContent = self.jpg2str(num, content_pic)
     return codeContent
 def seatsquery(self, month, address, codeContent):
     headers_seatsquery = headers_all(self.Cookie_str)["headers_seatsquery"]
     url = "https://toefl.etest.net.cn/cn/SeatsQuery?mvfAdminMonths=%s&mvfSiteProvinces=%s&whichFirst=AS&afCalcResult=%s&__act=__id.34.AdminsSelected.adp.actListSelected&submit.x=0&submit.y=0"%(month, address, codeContent)
     # print(len(url))
     response_query = self.tuoful_session.get(url ,headers = headers_seatsquery)
     text = response_query.content.decode("gb2312")
     print(text)
     return text,url
 def check_balances(self):
     # https://toefl.etest.net.cn/cn/MyHome/
     headers_myhome = headers_all(self.Cookie_str)["headers_myhome"]
     response = self.tuoful_session.get("https://toefl.etest.net.cn/cn/MyHome/",headers = headers_myhome)
     text = response.content
     selector = etree.HTML(text)
     data = selector.xpath('//*[@id="rightside"]//tr[1]/td[2]/text()')
     money = data[0].split(" ")[1]
     if int(money) >= 1761:
         return "money is enough"
     else:
         return "money is not enough"
 def checkTest(self, address):
     """
         功能:进入选择和查询页面得到有考位的信息
         参数:address:要查询的省份地址
         返回:res:有考位的信息,error:查询失败     
     """
     headers_query = headers_all(self.Cookie_str)["headers_query"]
     response_query = self.tuoful_session.get(
         "https://toefl.etest.net.cn/cn/Information?page=SeatsQuery",
         headers=headers_query)
     text = response_query.content.decode("gb2312")
     if "发生错误" in text:
         return "Access violation"
     #如果系统忙,休息1min后再次测试
     while self.checkBusy(text):
         print('系统繁忙,35s后再次测试')
         time.sleep(35)
         r = self.tuoful_session.get(
             "https://toefl.etest.net.cn/cn/Information?page=SeatsQuery",
             headers=headers_query)
         text = str(r.content)
     #判断是否进入正常考试查询页面
     if text.find('省份') == -1:
         print('进入选择页面失败!')
         if "please relogin first" in text:
             print("please relogin first")
             return "please relogin first"
         else:
             print("发生未知错误 请查看information.html文件")
             with open("information.html", "w") as f:
                 f.write(text)
             return "error"
     #获取验证码地址
     pattern1 = '<img src="/cn/'
     pattern2 = 'VerifyCode2.jpg'
     pos1 = text.find(pattern1)
     pos2 = text.find(pattern2)
     picUrl = 'https://toefl.etest.net.cn/' + text[
         pos1 + 11:pos2] + 'VerifyCode2.jpg'
     num = only_num()
     content_pic = self.tuoful_session.get(picUrl)  #获取验证码内容
     codeContent = self.jpg2str(num, content_pic)
     headers_result = headers_all(self.Cookie_str)["headers_result"]
     url = "https://toefl.etest.net.cn/cn/SeatsQuery?" + "mvfAdminMonths=%s" % self.month + "&mvfSiteProvinces=%s" % address + "&whichFirst=AS" + "&afCalcResult=" + str(
         codeContent
     ) + "&__act=__id.22.SeatsQuery.adp.actList" + "&submit.x=" + str(
         0) + "&submit.y=" + str(0)
     r = self.tuoful_session.get(url, headers=headers_result)
     text = r.content.decode("gb2312")
     #如果系统忙,休息30后再次测试
     while self.checkBusy(text):
         print('系统繁忙,35s后再次测试')
         time.sleep(30)
         url = "https://toefl.etest.net.cn/cn/SeatsQuery?" + "mvfAdminMonths=%s" % self.month + "&mvfSiteProvinces=%s" % address + "&whichFirst=AS" + "&afCalcResult=" + str(
             codeContent
         ) + "&__act=__id.22.SeatsQuery.adp.actList" + "&submit.x=" + str(
             0) + "&submit.y=" + str(0)
         r = self.tuoful_session.get(url, headers=headers_result)
         text = r.content.decode("gb2312")
     #判断是否进入正常考试查询页面
     if text.find('考位查询结果') == -1:
         print('进入查询页面失败!')
         if "Please login first" in text:
             print("Please login first")
             return "Please login first"
         elif ("请重新输入验证码" in text) or ("请输入验证码" in text):
             print("查询页面验证码错误")
             return "error_yz"
         else:
             print("发生未知错误 请查看kaowei.txt文件")
             with open("kaowei.txt", "w") as f:
                 f.write(text)
         return "error"
     address = address
     pattern = '有名额'
     testList = re.findall(pattern, text, re.S)
     if len(testList) != 0:
         res = self.deal_with(text, address)
     else:
         res = self.deal_with(text, address)
         print("%s%s没有考位" % (self.month, address))
     return res
Exemple #6
0
    def loginin(self):
        """
            功能:登录托福网站
            参数:None
            返回:ok:登陆成功,error:登录失败       
        """
        self.tuoful_session.cookies.clear()
        # self.tuoful_session.proxies = {'http':"120.77.35.48:8899"}
        # content = self.tuoful_session.get("http://httpbin.org/ip")
        # print(content.text)
        response = self.tuoful_session.get("http://toefl.etest.net.cn/cn/")
        text = response.content.decode("gb2312")
        if "登录" not in text:
            return "error"
        cook = response.cookies
        coo = {c.name: c.value for c in response.cookies}
        if "yunsuo_session_verify" in coo:
            return "error"
        else:
            temp_list = []
            for key, value in coo.items():
                srt = "%s=%s" % (key, value)
                temp_list.append(srt)
            self.Cookie_str = ";".join(temp_list)
        header_yz_login = headers_all(self.Cookie_str)["header_yz_login"]
        picUrl = "https://toefl.etest.net.cn/cn/" + str(int(
            time.time() * 1000)) + str(random.random())[1:] + "VerifyCode3.jpg"
        content_pic = self.tuoful_session.get(picUrl, headers=header_yz_login)
        num = only_num()
        #获取验证码内容
        codeContent = self.jpg2str(num, content_pic)
        headers_login = headers_all(self.Cookie_str)["headers_login"]
        s = str_md5("%s%s" % (self.neea_pwd, self.neea))
        x = s + codeContent
        pwd = str_md5(x)
        payload = {
            "username": "******" % self.neea,
            "__act": "__id.24.TOEFLAPP.appadp.actLogin",
            "password": pwd,
            "LoginCode": codeContent,
            "btn_submit.x": "0",
            "btn_submit.y": "0"
        }
        v = self.tuoful_session.post("https://toefl.etest.net.cn/cn/TOEFLAPP",
                                     data=payload,
                                     headers=headers_login)
        text = v.content.decode("gb2312")
        if "0; URL=/cn/MyHome/?" in text:
            headers_viewscores = {
                'Accept':
                'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
                'Accept-Encoding':
                'gzip, deflate, br',
                'Accept-Language':
                'zh-CN,zh;q=0.9',
                'Cache-Control':
                'max-age=0',
                'Connection':
                'keep-alive',
                'Cookie':
                self.Cookie_str,
                'Host':
                'toefl.etest.net.cn',
                'Referer':
                'https://toefl.etest.net.cn/cn/MyHome/?',
                'Upgrade-Insecure-Requests':
                '1',
                'User-Agent':
                'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36',
            }
            headers_ScoreReport = {
                'Accept':
                'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
                'Accept-Encoding':
                'gzip, deflate, br',
                'Accept-Language':
                'zh-CN,zh;q=0.9',
                'Cache-Control':
                'max-age=0',
                'Connection':
                'keep-alive',
                'Cookie':
                self.Cookie_str,
                'Host':
                'toefl.etest.net.cn',
                'Referer':
                'https://toefl.etest.net.cn/cn/ViewScores',
                'Upgrade-Insecure-Requests':
                '1',
                'User-Agent':
                'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36',
            }
            response = self.tuoful_session.get(
                "https://toefl.etest.net.cn/cn/ViewScores",
                headers=headers_viewscores)
            text = response.content
            selector = etree.HTML(text)
            data = selector.xpath('//*[@id="maincontent"]//tr[2]/td[2]/text()')
            if len(data) != 0:
                if data[0] == self.test_date:
                    score_link = selector.xpath(
                        '//*[@id="maincontent"]//tr[2]/td[4]/a/@href')
                    score_links = "https://toefl.etest.net.cn%s" % score_link[0]
                    response = self.tuoful_session.get(
                        score_links, headers=headers_ScoreReport)
                    text = response.content
                    selector = etree.HTML(text)
                    score_titles = selector.xpath(
                        '//*[@id="maincontent"]//tr[1]/td[@align="center"]/strong/text()'
                    )
                    score_title = score_titles[0:7]
                    score_res = selector.xpath(
                        '//*[@id="maincontent"]//tr[2]/td[@align="center"]/text()'
                    )
                    score_res = make_new_list(score_res[0:7])
                    # collection的名字:scoretasks
                    # 字段:openid,created,isFinished, test_date, neea, neea_pwd, isMatched, updated, result
                    # result是subCollection, reading, writing, listening, speaking
                    now = now = time.strftime('%Y-%m-%d %H:%M:%S',
                                              time.localtime(time.time()))
                    result = {
                        "openid": "%s" % self.openid,
                        "test_date": "%s" % deal_with_time(self.test_date),
                        "result": {
                            "reading": int(score_res[2]),
                            "listening": int(score_res[3]),
                            "speaking": int(score_res[4]),
                            "writing": int(score_res[5])
                        }
                    }
                    print(result)
                    self.mysql.update_result(result, self.openid)
                    return result
                return "没有出分"
            return "没有出分"

        elif "验证码错误" in text:
            now = time.strftime('%Y-%m-%d %H:%M:%S',
                                time.localtime(time.time()))
            print("%slogin验证码错误" % (now))
            return "error"
        elif "密码不正确" in text:
            now = time.strftime('%Y-%m-%d %H:%M:%S',
                                time.localtime(time.time()))
            print("%s密码不正确或验证码获取有误" % (now))
            return "error"
        else:
            print("发生未知错误 请查看login_error.txt文件")
            with open("login_error.txt", "w") as f:
                f.write(text)
            return "error"