国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

使用Selenium破解新浪微博的四宮格驗證碼

2020-01-04 14:18:29
字體:
來源:轉載
供稿:網友

在我們爬蟲的時候經常會遇到驗證碼,新浪微博的驗證碼是四宮格形式。

可以采用模板驗證碼的破解方式,也就是把所有驗證碼的情況全部列出來,然后拿驗證碼的圖片和這所有情況中的圖片進行對比,然后獲取驗證碼,再通過selenium自動拖拽點擊,進行破解。

Selenium,新浪微博,四宮格,驗證碼

我們將驗證碼四個點標注為1234,那么所有的情況就是以下24種情況。

數字代表箭頭指向:

1234 2134 3124 4321
1243 2143 3142 4312
1342 2314 3214 4123
1324 2341 3241 4132
1423 2413 3412 4213
1432 2431 3421 4231

所有的情況就是以上24種。我們將這24中驗證碼的情況放在一個文件夾內,當我們在登錄的時候用獲取的驗證碼截圖去和所有的情況一一對比,然后獲取完全相同的驗證碼,進行點擊即可。代碼如下:

from selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.by import Byfrom selenium.common.exceptions import TimeoutExceptionfrom selenium.webdriver.common.action_chains import ActionChainsimport timefrom PIL import Imagefrom io import BytesIOfrom os import listdirUSERNAME = ''PASSWORD = ''class CrackWeiboSlide():  def __init__(self):    self.url = 'https://passport.weibo.cn/signin/login'    self.browser = webdriver.Chrome()    self.wait = WebDriverWait(self.browser,20)    self.username = USERNAME    self.password = PASSWORD  def __del__(self):    self.browser.close()  def open(self):    """    打開網頁輸入用戶名密碼登錄    :return: None    """    self.browser.get(self.url)    username = self.wait.until(EC.presence_of_element_located((By.ID,'loginName')))    password = self.wait.until(EC.presence_of_element_located((By.ID,'loginPassword')))    submit = self.wait.until(EC.element_to_be_clickable((By.ID, 'loginAction')))    username.send_keys(self.username)    password.send_keys(self.password)    submit.click()  def get_position(self):    """    獲取驗證碼的位置    :return: 位置    """    try:      img = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME,'patt-shadow')))    except TimeoutException:      print('未出現驗證碼')      self.open()    time.sleep(2)    location = img.location    size = img.size    top=location['y']    bottom = location['y']+size['height']    left = location['x']    right = location['x']+size['width']    return (top,bottom,left,right)  def get_screenshot(self):    """    獲取截圖    :return:截圖    """    screentshot = self.browser.get_screenshot_as_png()    # BytesIO將網頁截圖轉換成二進制    screentshot = Image.open(BytesIO(screentshot))    return screentshot  def get_image(self,name):    """獲取驗證碼圖片"""    top,bottom,left,right = self.get_position()    print('驗證碼位置',top,bottom,left,right)    screenshot = self.get_screenshot()    # crop()將圖片裁剪出來,后面需要一個參數    captcha = screenshot.crop((left,top,right,bottom))    captcha.save(name)    return captcha  def detect_image(self,image):    """    匹配圖片    :param self:    :param image: 圖片    :return: 拖動順序    """    # 圖片所在的文件夾    for template_name in listdir('templates/'):      print('正在匹配',template_name)      template = Image.open('templates/'+template_name)      # 匹配圖片      if self.same_img(image,template):        # 將匹配到的文件名轉換為列表        numbers = [int(number)for number in list(template_name.split('.')[0])]        print('拖動順序',numbers)        return numbers  def is_pixel_equal(self,image1,image2,x,y):    """    判斷兩個像素的相似度    :param image1: 圖片1    :param image2: 圖片2    :param x: 位置x    :param y: 位置y    :return: 像素是否相同    """     # 取像素點    pixel1 = image1.load()[x,y]    pixel2 = image2.load()[x,y]    # 偏差量等于60    threshold = 60    if abs(pixel1[0]-pixel2[0]) < threshold and abs(pixel1[1]-pixel2[1])<threshold and abs(pixel1[2]-pixel2[2])<threshold:      return True    else:      return False  def same_img(self,image,template):    """    識別相似的驗證碼    :param image: 準備識別的驗證碼    :param template: 模板    :return:    """    # 相似度閾值    threshold = 0.99    count = 0    # 匹配所有像素點    for x in range(image.width):      for y in range(image.height):        # 判斷像素        if self.is_pixel_equal(image,template,x,y):          count+=1    result = float(count)/(image.width*image.height)    if result>threshold:      print('成功匹配')      return True    return False  def move(self,numbers):    """    根據順序拖動,此處接收的參數為前面的驗證碼的順序列表    :param numbers:    :return:    """    # 獲取四宮格的四個點    circles = self.browser.find_elements_by_css_selector('.patt-wrap .patt-circ')    print('-----------------',circles)    dx = dy =0    for index in range(4):      circle = circles[numbers[index]-1]      if index == 0:        # 點擊第一個點        ActionChains(self.browser).move_to_element_with_offset(circle,circle.size['width']/2,circle.size['height']/2).click_and_hold().perform()      else:        # 慢慢移動        times = 30        for i in range(times):          ActionChains(self.browser).move_by_offset(dx/times,dy/times).perform()          time.sleep(1/times)      if index == 3:        # 松開鼠標        ActionChains(self.browser).release().perform()      else:        # 計算下次的偏移        dx = circles[numbers[index+1]-1].location['x'] - circle.location['x']        dy = circles[numbers[index+1]-1].location['y'] - circle.location['y']  def crack(self):    """    破解入口    :return:    """    self.open()    # 獲取驗證碼圖片    image = self.get_image('captcha.png')    numbers = self.detect_image(image)    self.move(numbers)    time.sleep(10)    print('識別結束')if __name__ == '__main__':  crack = CrackWeiboSlide()  crack.crack()

設置自己的賬號密碼即可實現。

有時候會匹配不上,圖片相似度閾值達不到0.99以上,這個時候可能是我們收集的驗證碼圖片過時了,重新開啟圖片收集程序,運行收集一下即可。

收集圖片程序代碼如下:

from selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.by import Byfrom selenium.common.exceptions import TimeoutExceptionimport timefrom PIL import Imagefrom io import BytesIOfrom os import listdirUSERNAME = '18239831004'PASSWORD = 'qweqweqwe'class CrackWeiboSlide():  def __init__(self):    self.url = 'https://passport.weibo.cn/signin/login'    self.browser = webdriver.Chrome()    self.wait = WebDriverWait(self.browser,20)    self.username = USERNAME    self.password = PASSWORD  def __del__(self):    self.browser.close()  def open(self):    """    打開網頁輸入用戶名密碼登錄    :return: None    """    self.browser.get(self.url)    username = self.wait.until(EC.presence_of_element_located((By.ID,'loginName')))    password = self.wait.until(EC.presence_of_element_located((By.ID,'loginPassword')))    submit = self.wait.until(EC.element_to_be_clickable((By.ID, 'loginAction')))    username.send_keys(self.username)    password.send_keys(self.password)    submit.click()  def get_position(self):    """    獲取驗證碼的位置    :return: 位置    """    try:      img = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME,'patt-shadow')))    except TimeoutException:      print('未出現驗證碼')      self.open()    time.sleep(2)    location = img.location    size = img.size    top=location['y']    bottom = location['y']+size['height']    left = location['x']    right = location['x']+size['width']    return (top,bottom,left,right)  def get_screenshot(self):    """    獲取截圖    :return:截圖    """    screentshot = self.browser.get_screenshot_as_png()    # BytesIO將網頁截圖轉換成二進制    screentshot = Image.open(BytesIO(screentshot))    return screentshot  def get_image(self,name):    """獲取驗證碼圖片"""    top,bottom,left,right = self.get_position()    print('驗證碼位置',top,bottom,left,right)    screenshot = self.get_screenshot()    # crop()將圖片裁剪出來,后面需要一個參數    captcha = screenshot.crop((left,top,right,bottom))    captcha.save(name)    return captcha  # 獲取所有的驗證碼  def main(self):    count = 0    while True:      name = str(count)+'.png'      self.open()      self.get_image(name)      count+=1if __name__ == '__main__':  crack = CrackWeiboSlide()  crack.main()

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 万州区| 白水县| 济源市| 定日县| 建宁县| 天镇县| 濮阳县| 漾濞| 和田县| 南宫市| 砀山县| 资阳市| 常山县| 陇西县| 沁源县| 句容市| 余干县| 永丰县| 正定县| 石渠县| 平阴县| 乌鲁木齐市| 怀安县| 沿河| 四平市| 白水县| 化隆| 周口市| 根河市| 东宁县| 开原市| 于都县| 惠来县| 镇平县| 郴州市| 石台县| 碌曲县| 泰宁县| 北辰区| 普宁市| 望奎县|