项目:
通过Python爬虫自动化登入哔哩哔哩网页端账号
Python外部库
需要提前安装的库
import timefrom selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as Ecfrom selenium.webdriver.common.by import Byfrom chaojiying import Chaojiying_Clientfrom selenium.webdriver.common.action_chains import ActionChains
提示
1,需要在python包中的Scripts文件夹中下载与Google Chrome浏览器版本相同的浏览器驱动
2,使用了超级鹰的验证码技术需要提前注册超级鹰账号
超级鹰官方网站:http://www.chaojiying.com
项目代码
import timefrom selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as Ecfrom selenium.webdriver.common.by import Byfrom chaojiying import Chaojiying_Clientfrom selenium.webdriver.common.action_chains import ActionChainsclass bilibili: def __init__(self): # 构造方法或者初始化方法 self.driver = webdriver.Chrome() self.url = 'https://www.bilibili.com/' self.driver.maximize_window() def Verification_code_image(self): self.driver.get(self.url) # 单击导航栏的登图标 弹出登录模块 WebDriverWait(self.driver, 10).until(Ec.presence_of_element_located((By.CLASS_NAME, 'header-login-entry'))) self.driver.find_element_by_class_name('header-login-entry').click() """哔哩哔哩的 账号 密码""" user_name = '写入账号' user_pwd = '写入密码' # 定位账号块并输入账号 WebDriverWait(self.driver, 20).until( Ec.presence_of_element_located((By.XPATH, '/html/body/div[3]/div/div[4]/div[2]/form/div[1]/input'))) self.driver.find_element_by_xpath('/html/body/div[3]/div/div[4]/div[2]/form/div[1]/input').send_keys(user_name) # 定位密码块并输入密码 WebDriverWait(self.driver, 10).until( Ec.presence_of_element_located((By.XPATH, '/html/body/div[3]/div/div[4]/div[2]/form/div[3]/input'))) self.driver.find_element_by_xpath('/html/body/div[3]/div/div[4]/div[2]/form/div[3]/input').send_keys(user_pwd) # 单击登录键 :要等待5秒在单击登录键,不然验证码图片加载不出来 time.sleep(5) self.driver.find_element_by_xpath('/html/body/div[3]/div/div[4]/div[2]/div[2]/div[2]').click() # 抠图 等待 :截取验证码图片 WebDriverWait(self.driver, 10).until( Ec.presence_of_element_located((By.CLASS_NAME, 'geetest_holder.geetest_silver'))) self.driver.find_element_by_class_name('geetest_holder.geetest_silver').screenshot('哔哩.png') def dock_chaojiying(self): # 使用超级鹰识别点选验证码图片 chaojiying = Chaojiying_Client('超级鹰账号', '超级鹰密码', '软件ID') im = open('哔哩.png', 'rb').read() result = chaojiying.PostPic(im, '9004') #9004是验证码类型编号 # 坐标多选,返回1~4个坐标,如:x1,y1|x2,y2|x3,y3>>[[x1,y1],[x2,y2],[x3,y3]] coordinates = result.get('pic_str').split('|') locations = [[int(number) for number in coordinate.split(',')] for coordinate in coordinates] # print(locations) # 以列表形式返回 坐标 return locations def simulated_click(self): self.Verification_code_image() # 定位验证码图片 并使用超级鹰返回的坐标 按顺序单击正确的验证码 img = WebDriverWait(self.driver, 10).until( Ec.presence_of_element_located((By.CLASS_NAME, 'geetest_holder.geetest_silver'))) for location in self.dock_chaojiying(): ActionChains(self.driver).move_to_element_with_offset(img, location[0], location[1]).click().perform() time.sleep(2) # 点击确定按钮 self.driver.find_element_by_class_name('geetest_commit_tip').click() time.sleep(2) print("登入成功")if __name__ == '__main__': bi = bilibili() # 实例化bilibili类 bi.simulated_click()