Python自動化登山天眼検査データの実現


まずアカウントのパスワードを登録して、アカウントのパスワードを通じて登録して、そしてスライダの検証を通して、自動的に検索キーワードを入力して、ジャンプしてページをめくってデータを取って、そしてExcelファイルの中に保存します。
コードを実行すると、スライダの検証が常に通らず、食べられてしまいますが、exe運転にパッケージ化されていることが分かりました。100%登録に成功しました。もし皆さんがこの問題を知っているなら、私と共有してください。ありがとうございます。

余計なことを言わないで、直接コードを入れてください。

# coding=utf-8
from selenium import webdriver
import time
from PIL import Image, ImageGrab
from io import BytesIO
from selenium.webdriver.common.action_chains import ActionChains
import os
import sys
import re
import xlwt
import urllib
import datetime
 
'''
         ,        
'''
 
#        
def app_path():
    if hasattr(sys, 'frozen'):
        return os.path.dirname(os.path.dirname(os.path.dirname(sys.executable))) #  pyinstaller    exe  
    return os.path.dirname(__file__)
 
app_path = app_path()
 
ready_list = []
 
#      
def set_style(name,height,bold=False):
    style = xlwt.XFStyle()
    font = xlwt.Font()
    font.name = name
    # font.bold = bold
    font.color_index = 4
    font.height = height
    style.font = font
    return style
 
#  excel
f = xlwt.Workbook()
sheet1 = f.add_sheet('     ',cell_overwrite_ok=True)
row0 = ["    ","     ","    ","    ","  ","  ","  "]
for i in range(0, len(row0)):
    sheet1.write(0, i, row0[i], set_style('Times New Roman', 220, True))
 
#   
def write_col(data, row, col):
    for i in range(0,len(data)):
        sheet1.write(row,col,data[i],set_style('Times New Roman',220,True))
        row = row + 1
 
def parse_save_data(all_list):
    row = 1
    for data in all_list:
        #     
        name_list = re.findall(r'<div class="info">(.*?)</div>',data)
        print(name_list)
 
        #   
        tag_list = re.findall(r'<div class="tag-list">(.*)</div><div class="info row text-ellipsis">', data)
        tags = []
        for list in tag_list:
            tag = re.findall(r'<div class="tag-common -primary -new">(.*?)</div>', list)
            tags.append(tag)
        # print(tags)
 
        #      
        legal_list = re.findall(r'<a title="(.*?)" class="legalPersonName link-click"',data)
        # print(legal_list)
 
        #     
        registered_capital_list  = re.findall(r'    :<span title="(.*?)">',data)
        # print(registered_capital_list)
 
        #     
        date_list  = re.findall(r'    :<span title="(.*?)">',data)
        # print(date_list)
 
        #   
        tel_list  = re.findall(r'<div class="triangle" style=""></div><div class=""></div></div></div><span>(.*?)</span>',data)
        # print(tel_list)
 
        #   
        email_list  = re.findall(r'  :</span><span>(.*?)</span>',data)
        # print(email_list)
 
        #   
        adress_list  = re.findall(r'  :</span><span>(.*?)</span>',data)
        # print(adress_list)
        write_col(name_list,row,0)
        # write_col(tags,1)
        write_col(legal_list,row,1)
        write_col(registered_capital_list,row,2)
        write_col(date_list,row,3)
        write_col(tel_list,row,4)
        write_col(email_list,row,5)
        write_col(adress_list,row,6)
 
        row = row + len(name_list)
 
    s = str([datetime.datetime.now()][-1])
    name = '/     ' + s[:10] + s[-6:] + '.xls'
    f.save(app_path + name)
 
def get_track(distance):
    """
               
    :param distance:    
    :return:     
    """
    #     
    track = []
    #     
    current = 0
    #     
    mid = distance * 2 / 5
    #     
    t = 0.2
    #    
    v = 1
 
    while current < distance:
        if current < mid:
            #      2
            a = 5
        else:
            #      3
            a = -2
        #    v0
        v0 = v
        #     v = v0 + at
        v = v0 + a * t
        #     x = v0t + 1/2 * a * t^2
        move = v0 * t + 1 / 2 * a * t * t
        #     
        current += move
        #     
        track.append(round(move))
    return track
 
 
def autologin(account, password):
    count = 0
    global driver,page,keywords
    driver.get('https://www.tianyancha.com/?jsid=SEM-BAIDU-PP-SY-000873&bd_vid=7864822754227867779')
    time.sleep(3)
    try:
        driver.find_element_by_xpath('//*[@id="tyc_banner_close"]').click()
    except:
        pass
 
    driver.find_element_by_xpath('//div[@class="nav-item -home  -p10"]/a').click()
    time.sleep(3)
    #           id xpath      ,     id      ,       class  
    driver.find_element_by_xpath('.//div[@class="sign-in"]/div/div[2]').click()
    time.sleep(1)
    accxp = './/input[@id="mobile"]'
    pasxp = './/input[@id="password"]'
    driver.find_element_by_xpath(accxp).send_keys(account)
    driver.find_element_by_xpath(pasxp).send_keys(password)
    clixp = './/div[@class="sign-in"]/div[2]/div[2]'
    driver.find_element_by_xpath(clixp).click()
    #                
    time.sleep(2)
    img = driver.find_element_by_xpath('/html/body/div[10]/div[2]/div[2]/div[1]/div[2]/div[1]')
    time.sleep(0.5)
    #          
    location = img.location
    size = img.size
    #                  
    top, bottom, left, right = location['y'], location['y'] + size['height'], location['x'], location['x'] + size[
        'width']
    #        (    )
    screenshot = driver.get_screenshot_as_png()
    screenshot = Image.open(BytesIO(screenshot))
    captcha1 = screenshot.crop((left, top, right, bottom))
    print('--->', captcha1.size)
    captcha1.save('captcha1.png')
    #        (    )
    driver.find_element_by_xpath('/html/body/div[10]/div[2]/div[2]/div[2]/div[2]').click()
    time.sleep(4)
    img1 = driver.find_element_by_xpath('/html/body/div[10]/div[2]/div[2]/div[1]/div[2]/div[1]')
    time.sleep(0.5)
    location1 = img1.location
    size1 = img1.size
    top1, bottom1, left1, right1 = location1['y'], location1['y'] + size1['height'], location1['x'], location1['x'] + \
                                   size1['width']
    screenshot = driver.get_screenshot_as_png()
    screenshot = Image.open(BytesIO(screenshot))
    captcha2 = screenshot.crop((left1, top1, right1, bottom1))
    captcha2.save('captcha2.png')
    #      
    left = 55  #            
    for i in range(left, captcha1.size[0]):
        for j in range(captcha1.size[1]):
            #            
            pixel1 = captcha1.load()[i, j]
            pixel2 = captcha2.load()[i, j]
            threshold = 60
            if abs(pixel1[0] - pixel2[0]) < threshold and abs(pixel1[1] - pixel2[1]) < threshold and abs(
                    pixel1[2] - pixel2[2]) < threshold:
                pass
            else:
                left = i
    print('    ', left)
    #       
    left -= 52
    #     
    track = get_track(left)
    print('    ', track)
    # track += [5,4,5,-6, -3,5,-2,-3, 3,6,-5, -2,-2,-4]  #        ,       
    #     
    slider = driver.find_element_by_xpath('/html/body/div[10]/div[2]/div[2]/div[2]/div[2]')
    ActionChains(driver).click_and_hold(slider).perform()
    for x in track:
        ActionChains(driver).move_by_offset(xoffset=x, yoffset=0).perform()
    time.sleep(0.2)
    ActionChains(driver).release().perform()
    time.sleep(1)
    try:
        if driver.find_element_by_xpath('/html/body/div[10]/div[2]/div[2]/div[2]/div[2]'):
            print('     ,   ')
            # driver.delete_all_cookies()
            # driver.refresh()
            # autologin(driver, account, password)
        else:
            print('login success')
    except:
        print('login success')
 
    time.sleep(0.2)
    driver.find_element_by_xpath('.//input[@id="home-main-search"]').send_keys(keywords)
    driver.find_element_by_xpath('.//div[@class="input-group home-group"]/div[1]').click()
 
    #    
    data = driver.find_element_by_xpath('.//div[@class="result-list sv-search-container"]').get_attribute('innerHTML')
    count = count + 1
 
    #        
    ready_list.append(data)
 
    while count < page:
        #      
        # driver.find_element_by_xpath('./ul[@class="pagination"]]/li/a[@class="num -next"]').click()
        url = 'https://www.tianyancha.com/search/p{}?key={}'.format(count + 1,urllib.parse.quote(keywords))
        driver.get(url)
        time.sleep(2)
        data = driver.find_element_by_xpath('.//div[@class="result-list sv-search-container"]').get_attribute('innerHTML')
        count = count + 1
        ready_list.append(data)
 
    #       
    parse_save_data(ready_list)
    print('      ')
 
        # if __name__ == '__main__':
    # driver_path = 'C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe'
    # chromeoption = webdriver.ChromeOptions()
    # chromeoption.add_argument('--headless')
    # chromeoption.add_argument('user-agent='+user_agent)
 
keywords = input('      :')
account = input('        :')
password = input('        :')
page = int(input('       :'))
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(10)
print('      。。。')
autologin(account, password)
 
exeに包装します。(site-packagesは自分のpythonパッケージのディレクトリに変えます。)

pyinstaller main.py -p D:\Anaconda3\Lib\site-packages

最終的にdistディレクトリのexeを実行します。

注意事項
天眼検査は会員がいないので、4ページの内容しか確認できません。会員が必要です。これは迂回したいので、別途研究しなければなりません。結局チャージ料がかかります。解読もそんなに簡単ではありません。
ここでPython自動化に関する天眼検査データの記事を紹介します。これに関連して、Python自動化天眼検査データの内容は以前の文章を検索してください。または下記の関連記事を引き続きご覧ください。これからもよろしくお願いします。