示例#1
0
文件: base.py 项目: Yuan-zewei/jzsc
 def jichutoken(self):
     try:
         while True:
             a = Mysql.jichutoken(yxq='0')
             if a:
                 return a
             else:
                 time.sleep(5)
     except Exception as e:
         print(e, '文件错误')
示例#2
0
def selenu(url, qyname, ip):
    print(f'开始尝试')
    caps = DesiredCapabilities.CHROME
    caps['loggingPrefs'] = {'performance': 'ALL'}
    caps = {
        'browserName': 'chrome',
        'loggingPrefs': {
            'browser': 'ALL',
            'driver': 'ALL',
            'performance': 'ALL',
        },
        'goog:chromeOptions': {
            'perfLoggingPrefs': {
                'enableNetwork': True,
            },
            'w3c': False,
        },
    }
    chromeOptions = webdriver.ChromeOptions()
    chromeOptions.add_experimental_option('w3c', False)
    chromeOptions.add_experimental_option(
        'excludeSwitches',
        ['enable-automation'])  #开始实验性功能非常牛叉的参数,防止网页发现你是selenuim
    chromeOptions.add_argument('--headless')
    chromeOptions.add_argument(f'--proxy-server=http://{ip}')  #隐藏浏览器
    driver = webdriver.Chrome(options=chromeOptions, desired_capabilities=caps)
    driver.maximize_window()
    driver.set_page_load_timeout(40)  #超过这个时间直接报错
    driver.get(f'http://jzsc.mohurd.gov.cn/data/company/detail?id={url}')
    a = 1
    while True:
        try:
            time.sleep(3)
            he1 = driver.page_source
            time.sleep(1)
            if he1.find('重新验证') != -1 and he1.find(f'{qyname}') == -1:
                # driver.switch_to.window(driver.window_handles[0])#切换窗口发现没啥用
                time.sleep(3)
                tijiao = driver.find_element_by_xpath(
                    '//*[@id="app"]/div/header/div[5]/div/div[3]/div/button[1]/span'
                )
                driver.execute_script("arguments[0].click();", tijiao)
                time.sleep(1)
                # driver.switch_to.window(driver.window_handles[0])#切换窗口
                hem = driver.page_source
                time.sleep(1)
                for ui in range(0, 6):
                    if hem.find('请完成安全验证') != -1 or hem.find(
                            f'{qyname}') == -1:
                        current_time = time.strftime(
                            "%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
                        current_time1 = time.strftime(
                            "%Y-%m-%d", time.localtime(time.time()))
                        time.sleep(0.5)
                        imgelement = driver.find_element_by_xpath(
                            '/html/body/div[2]/div[2]/div/div/div[2]')
                        locations = imgelement.location
                        sizes = imgelement.size
                        rangle = (int(locations['x'] + 20),
                                  int(locations['y'] + 20),
                                  int(locations['x'] + sizes['width'] - 20),
                                  int(locations['y'] + sizes['height'] - 20))
                        pfilename = '.\\image'  #路径错的话使用绝对路径
                        save_path = pfilename + '\\' + current_time1 + '_' + current_time + '.png'
                        time.sleep(1.5)
                        driver.save_screenshot(save_path)
                        img = Image.open(save_path)
                        jpg = img.convert('RGB')
                        jpg = img.crop(rangle)
                        path = pfilename + '\\' + current_time1 + '_' + current_time + '.png'
                        time.sleep(1)
                        jpg.save(path)
                        print("图片截取成功!")
                        chaojiying = Chaojiying_Client('账号', '密码',
                                                       '软件id')  # 用户中心>>软件ID
                        im = open(path, 'rb').read()
                        zuo = chaojiying.PostPic(im, 9103)
                        groups = zuo.get('pic_str').split('|')
                        locations_chaojiying = [[
                            int(number) for number in group.split(',')
                        ] for group in groups]
                        if len(locations_chaojiying) > 0:
                            element = WebDriverWait(driver, 5, 0.5).until(
                                EC.presence_of_element_located(
                                    (By.CLASS_NAME, 'yidun_bg-img')))
                            ActionChains(driver).move_to_element(element)
                            time.sleep(0.5)
                            location_x = 0
                            location_y = 0
                            pyautogui.moveTo(locations['x'] + 25,
                                             int(locations['y'] + 96),
                                             duration=0.3)  #驱动鼠标操作,可以使用,只是看看
                            for location in locations_chaojiying:
                                pyautogui.moveRel(location[0] - location_x,
                                                  location[1] - location_y,
                                                  duration=0.6)
                                driver.execute(Command.MOVE_TO, {
                                    'xoffset': location[0],
                                    'yoffset': location[1]
                                })
                                print(" 点击坐标 " + str(location[0]),
                                      str(location[1]))
                                ActionChains(
                                    driver).move_to_element_with_offset(
                                        element, location[0],
                                        location[1] + 2).click().perform()
                                time.sleep(
                                    random.randint(1, 3) + random.random())
                                location_x = location[0]
                                location_y = location[1]
                        time.sleep(10)  #防止网页加载速度过慢拿不到公司名字
                        print('移动成功')
                        hem12 = driver.page_source
                        if hem12.find(f'{qyname}') != -1:
                            print('跳过验证码')
                            logs = [
                                json.loads(log['message'])['message']
                                for log in driver.get_log('performance')
                            ]
                            token = re.findall(
                                "accessToken': '(.*?)==', 'timeout': '30000'",
                                str(logs))[-1] + '=='
                            a21 = Mysql.seletoken(token=token)
                            if a21:
                                print('token已存在跳过')
                            else:
                                Mysql.insert_token(token=token, ip=ip)
                                a = 0
                            while True:
                                a12 = Mysql.jichutoken(yxq='0')
                                if a12:
                                    print('token获得成功暂停5秒钟', token)
                                    time.sleep(5)
                                else:
                                    driver.refresh()
                                    break
                        else:
                            print('验证失败或者没有这个公司重新尝试')
                            time.sleep(3)
                            break
                            # driver.refresh()
            elif he1.find(f'{qyname}') != -1:
                logs = [
                    json.loads(log['message'])['message']
                    for log in driver.get_log('performance')
                ]
                token = re.findall(
                    "accessToken': '(.*?)==', 'timeout': '30000'",
                    str(logs))[-1] + '=='
                a21 = Mysql.seletoken(token=token)
                if a21:
                    print('token已存在跳过')
                else:
                    Mysql.insert_token(token=token, ip=ip)
                    a = 0
                while True:
                    time.sleep(2)
                    a12 = Mysql.jichutoken(yxq='0')
                    if a12:
                        print('token获得成功暂停5秒钟', token)
                        time.sleep(5)
                    else:
                        driver.refresh()
                        break
            elif he1.find(f'{qyname}') == -1:
                print(f'2第{a}次刷新')
                driver.refresh()
                break
            else:
                time.sleep(1.5)
                driver.refresh()
                break
        except Exception as e:
            print(e)
            driver.quit()
            break