使用selenium+django查询个人成绩

网络爬虫 2018-07-24 1100

from selenium import webdriver  
from PIL import Image  
from selenium.common.exceptions import NoSuchElementException  
from selenium.webdriver.support.select import Select  
from aip import AipOcr  
from time import sleep

browser = webdriver.Chrome()  
url = 'http://jwc2.yangtzeu.edu.cn:8080/'  
browser.maximize_window()  
# 读取图片  
filePath = 'D:\\2.png'  

def get_login(url):  
    browser.get(url)  
    input = browser.find_element_by_id('txtUid')  
    input.send_keys('#')  
    input2 = browser.find_element_by_id('txtPwd')  
    input2.send_keys('111')  
    Select(browser.find_element_by_name("selKind")).select_by_value("1")  
    input3 = browser.find_element_by_id('txtCheckCode')  
    try:  
        browser.get_screenshot_as_file('D:\\1.png')  
        img = Image.open('D:\\1.png')  
        img2 = img.crop((990, 470, 1100, 510))  
        img2.save('D:\\2.png')  
    except NoSuchElementException as e:  
        print(e)  
    result = deal_data()  
    input3.send_keys(result)  
    sleep(3)  
    browser.find_element_by_name('btLogin').click()  
    fail_text = browser.find_element_by_id('lbCheckCode')  
    if fail_text.text == '验证码错误,请重新输入验证码!':  
        return get_login(url)  
    else:  
        browser.execute_script('window.open()')  
        browser.switch_to_window(browser.window_handles[1])  
        browser.get('http://jwc2.yangtzeu.edu.cn:8080/cjcx.aspx')  
        browser.find_element_by_name('btAllcj').click()  

def ocr_deal():  
    APP_ID = '#'  
    API_KEY = '#'  
    SECRET_KEY = '#'  

    # 初始化AipFace对象  
    aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)  
    return aipOcr  

def get_file_content(filePath):  
    with open(filePath, 'rb') as fp:  
        return fp.read()  

def deal_data():  
    aipOcr = ocr_deal()  
    options = {}  
    options["language_type"] = "CHN_ENG"  
    options["detect_direction"] = "true"  
    options["detect_language"] = "true"  

    # 调用通用文字识别接口  
    result = aipOcr.general(get_file_content(filePath), options)  
    for i in result['words_result']:  
        x = i['words']  
        with open('D:\\result.txt', 'w') as f:  
            f.write(x)  
            f.close()  

    text = open('D:\\result.txt').read().strip()  
    return text  
get_login(url)  

加入django显示 

def home(request):  
    data = get_data()  
    n = 6  
    y = []  
    x = [data[i:i + n] for i in range(0, len(data), n)]  
    for i in x:  
        k = {}  
        k[crouse_name] = i[0]  
        k[score] = i[1]  
        k[stu_score] = i[2]  
        k[year] = i[3]  
        k[term] = i[4]  
        k[crouse_type] = i[5]  
        y.append(k)  
    print(y)  
    context = {}  
    context[k] = y  
    return render(request, home.html, context)  

与之前只是多了一个首页显示

标签:网络爬虫

文章评论

评论列表

已有0条评论