zoukankan      html  css  js  c++  java
  • 91家纺网,模拟浏览器登录

    import re
    import os
    import base64
    #import ast
    from urllib import parse
    from datetime import datetime
    import cv2

    import random 
    import requests
    import time
    import redis
    from scrapy import Selector

    from selenium import webdriver
    from selenium.webdriver import ActionChains
    from PIL import Image

    domain = "http://www.91jf.com/"
    domain_log = "http://www.91jf.com/default.php?url=member&act=memberinfo"
    #IMAGE_URL = "http://www.91jf.com/default.php?act=code"

    chrome_options = webdriver.ChromeOptions()
    path = r'D:project_91jiafangwanchromedriver.exe'
    browser = webdriver.Chrome(executable_path=path, chrome_options=chrome_options)


    def get_captcha(filename):
        """
        访问验证码解析API,解析图片验证码
        :param filename:
        :return:
        """
        CAPTCHA_PATH = 'http://192.168.3.9:8888/captcha'
        with open(filename, "rb") as f:
            image_base64 = base64.b64encode(f.read())
        payload = {'image': image_base64}
        while True:
            response = requests.request("POST", CAPTCHA_PATH, data=payload)
            if response.status_code == 200:
                if response.json().get('code') == 200:
                    return response.json().get('text')
                else:
                    print(response.json().get('err'))
                    return '1234'
            else:
                print('验证码网络无法访问,请确认网络情况')
                time.sleep(10)

    def log_in():
        browser.get(domain_log)
        browser.maximize_window()

        slider = browser.find_element_by_xpath("//img[@onclick='show_user_login()']")
        ActionChains(browser).click(slider).perform()
        ActionChains(browser).release().perform()

        #IMAGE_URL = "http://www.91jf.com/default.php?act=code"
        img = browser.find_element_by_xpath("//img[@src='default.php?act=code']")

        username = "18206275210"
        password = "czx0711."

        username_ele = browser.find_element_by_xpath("//input[@name='username']")
        password_ele = browser.find_element_by_xpath("//input[@name='password']")

        username_ele.send_keys(username)
        password_ele.send_keys(password)

        time.sleep(2)
        #request_download(IMAGE_URL)

        #image = cv2.imread('./image/img0.jpg')
        cv2.imshow("image",img)
        cv2.waitKey(0)
        
        pic = get_captcha('./image/img0.jpg')
        pic_ele = browser.find_element_by_xpath("//input[@name='t_code']")
        pic_ele.send_keys(pic)
        
        time.sleep(2)

        slider = browser.find_element_by_xpath("//div[@class='login_but']")
        ActionChains(browser).click(slider).perform()
        ActionChains(browser).release().perform()


    def request_download(IMAGE_URL):
        os.makedirs('./image/', exist_ok=True)
        import requests
        r = requests.get(IMAGE_URL)
        with open('./image/img'+ '0' + '.jpg', 'wb') as f:
            f.write(r.content) 
    '''
    for i in range(1000):
        request_download(str(i))
        time.sleep(random.randint(1,4))
        '''
    log_in()

    '''
    image = cv2.imread('./image/img0.jpg')
    cv2.imshow("image",image)
    cv2.waitKey(0)
    '''
  • 相关阅读:
    算法--判断数组中是否有重复值
    算法--小范围排序
    Spark性能调优之JVM调优
    算法-java代码实现基数排序
    算法-java代码实现计数排序
    算法-java代码实现希尔排序
    算法-java代码实现堆排序
    Kafka集群的搭建
    深度学习必备包
    Keras 学习之旅(一)
  • 原文地址:https://www.cnblogs.com/dog-and-cat/p/13307084.html
Copyright © 2011-2022 走看看