注意:這里采用的是新版的selenium有關事項
1.舊版FindsByID、FindsByCss等已被刪除,并替換為Find_element(By.ID,' '),Find_elements(By.Xpath," ")等實例。
2.move_to_element_with_offset
的方法定位基準位置從左上角修改為中心
導入相關包
1.創(chuàng)建瀏覽器對象,訪問網(wǎng)址,點擊登錄圖標,輸入賬號、密碼,點擊登錄
? 觀察登錄圖標,賬號、密碼,登錄按鈕相關的元素
使用find_element()方法來獲取上面提到的各個元素
2.獲取驗證碼相關截圖:
點擊登錄后,可以得到驗證碼的元素
用screenshot()方法截下這張圖
3.使用超級鷹識別出驗證碼的相關坐標
下載壓縮包
下載完會有一個chaojiying.py程序,我們導入到我們的的案例中,
調(diào)用包中的相關方法
修改上文的參數(shù),注:'軟件ID'要去用戶中心-軟件ID新建一個。
4.使用動作鏈對驗證碼圖片進行點擊事件
上面的result返回的是坐標
我們需要對坐標使用split()方法進行拆分后遍歷得到每一組的x,y數(shù)值,然后新建一個動作鏈對象,
調(diào)用move_to_element_with_offset(驗證碼元素,x坐標,y坐標)方法
(注:新版的selenium把基準位置從左上角改為了中心所以方法里的x,y坐標需要減少長,寬的1/2。)
5.完成登錄
點擊確認,完成登錄
文章來源:http://www.zghlxwxcb.cn/news/detail-764056.html
源碼:文章來源地址http://www.zghlxwxcb.cn/news/detail-764056.html
from selenium import webdriver from time import sleep from PIL import Image from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By from 動態(tài)加載數(shù)據(jù)處理.chaojiying import Chaojiying_Client bro=webdriver.Edge() bro.get('https://www.bilibili.com/?spm_id_from=444.41.0.0') bro.maximize_window() button_login=bro.find_element(By.CLASS_NAME,'header-login-entry') button_login.click() sleep(2) account=bro.find_element(By.XPATH,'/html/body/div[4]/div/div[4]/div[2]/form/div[1]/input') account.send_keys('賬號') password=bro.find_element(By.XPATH,'/html/body/div[4]/div/div[4]/div[2]/form/div[3]/input') password.send_keys('密碼') sleep(2) button_denglu=bro.find_element(By.CLASS_NAME,'btn_primary') button_denglu.click() sleep(1) # 對當前頁面進行截圖保存 # bro.save_screenshot('aa.png') # sleep(2) #確定驗證碼圖片對應的左上角和右下角坐標(裁剪對應的區(qū)域) img_element=bro.find_element(By.CLASS_NAME,'geetest_widget') img_element.screenshot('yzm.png') # location=img_element.location#loction_img返回的是字典{x:?,y:?},返回的事x,y坐標 # size=code_img_ele.size#size返回的也是字典{height:?,width:?},返回的是高度,寬度坐標 # #將左上角和右下角的坐標放入一個元組里 # range1=(int(location['x']),int(location['y']),int(location['x']+size['width']), # int(location['y']+size['height'])) # #至此整張圖片區(qū)域已經(jīng)確定 # sleep(2) # i=Image.open('./aa.png') # #裁剪后的名字 # code_img_name = './yzm.png' # #開始裁剪 # frame=i.crop(range1) # #裁剪后保存到文件里 # frame.save(code_img_name) sleep(1) chaojiying = Chaojiying_Client('賬號', '密碼', '軟件id') # 用戶中心>>軟件ID 生成一個替換 96001 im = open('yzm.png', 'rb').read() # 本地圖片文件路徑 來替換 a.jpg 有時WIN系統(tǒng)須要// result=chaojiying.PostPic(im,9004)['pic_str'] print(result) list=result.split('|') img_element_half_width=float(img_element.size['width'])/2 img_element_half_height=float(img_element.size['height'])/2 for li in list: x=int(li.split(',')[0]) y=int(li.split(',')[1]) action=ActionChains(bro) action.move_to_element_with_offset(img_element,int(x-img_element_half_width), int(y-img_element_half_height)).click().perform() #img_element是驗證碼元素框,x,y是坐標 sleep(1) button_confirm=bro.find_element(By.CLASS_NAME,'geetest_commit') button_confirm.click() sleep(3) bro.close()
到了這里,關于Python爬蟲+selenium+超級鷹實現(xiàn)自動登錄b站(最新可用版)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!