爬虫Selenium批量注册源码***

import random
import yundama
import time
from PIL import Image
from io import BytesIO

from 随机生成身份证号 import getRandomIdCard
from selenium import webdriver
from 随机生成一个姓名 import Name

while True:
# driver = webdriver.ChromeOptions() # 创建一个配置对象
driver = webdriver.Chrome() # 实例化带有配置的driver对象
url = ‘http://www.4399.com/’
driver.get(url)
# driver.switch_to.frame(‘login_frame’)

buttonss = driver.find_element_by_xpath('//*[@id="login_toregister"]')
buttonss.click()
driver.switch_to.frame('popup_reg_frame')
seed = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
sa = []
for i in range(8):
    sa.append(random.choice(seed))
salt = ''.join(sa)
# print(salt)
username = driver.find_element_by_id('username').send_keys(salt)
el_user = driver.find_element_by_name('password')
num = 'qwerty{}'.format(random.randint(0,9))
el_user.send_keys(num)
time.sleep(2)

el_user2 = driver.find_element_by_id('passwordveri').send_keys(num)
time.sleep(2)
el_name = driver.find_element_by_id('realname').send_keys('{}'.format(Name()))
time.sleep(2)
el_name_id = driver.find_element_by_id('idcard').send_keys(getRandomIdCard())
time.sleep(2)
# driver.save_screenshot('getimage.jpg')
# 截图
screentshot1 = driver.get_screenshot_as_png()
with open('getimage1.jpg','wb') as f:
    f.write(screentshot1)
# 使用PIL将截图创建成图片对象,该对想可以获取图片的相关信息
screentshot = Image.open(BytesIO(screentshot1))

captcha2 = screentshot.crop([479,390,542,425])

with open('getimage.png', 'wb')as f:
    captcha2.save(f)
with open('getimage.png', 'rb')as f:
    halou = f.read()

    try:
        wa = yundama.identify(halou)
        print(wa)
        print('************************************************************')
        driver.find_element_by_xpath('//*[@id="inputCaptcha"]').send_keys(wa)
    except :
        print('没有验证码')

    try:
        deng_button =driver.find_element_by_xpath('//*[@id="reg_form"]/fieldset/div[9]/input')
        #//*[@id="reg_form"]/fieldset/div[10]/input
        deng_button.click()
        time.sleep(1)
    except:
        print('标签错误,正在重新更换标签lodaing')
        deng_button = driver.find_element_by_xpath('//*[@id="reg_form"]/fieldset/div[10]/input')
        # //*[@id="reg_form"]/fieldset/div[10]/input
        deng_button.click()
        time.sleep(1)
    else:
        pass
    finally:
       
        print('创建成功')
        userpassword_list=[]
        u=driver.find_element_by_xpath('/html/body/div/div/div[2]/div/ul/li[1]').text
        print(u)
        userpassword_list.append(u)
        p = driver.find_element_by_xpath('/html/body/div/div/div[2]/div/ul/li[2]').text
        print(p)
        userpassword_list.append(p)
        time.sleep(1)
        with open('4399.text','a') as f:
            f.writelines(str(userpassword_list)+'\n')
        driver.quit()

你可能感兴趣的:(爬虫)