欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python爬蟲如何解決圖片驗(yàn)證碼

 更新時(shí)間:2021年02月14日 09:28:32   作者:青衫不改就人還  
這篇文章主要介紹了python爬蟲如何解決圖片驗(yàn)證碼,幫助大家更好的理解和使用python,感興趣的朋友可以了解下

之前剛開始做爬蟲的時(shí)候遇到過登錄驗(yàn)證碼問題,看過很多帖子都沒有解決我的問題,發(fā)現(xiàn)大多數(shù)帖子都是治標(biāo)不治本,于是想分享一下自己的解決方案。本次采用的網(wǎng)站是古詩(shī)文網(wǎng),使用百度API,因?yàn)榘俣華PI免費(fèi)!免費(fèi)!免費(fèi)!適合自己學(xué)習(xí)的時(shí)候使用。如果還沒有使用過百度API識(shí)別驗(yàn)證碼的朋友可以看一下我的這個(gè)帖子。
以下案例采用的時(shí)古詩(shī)文網(wǎng):登錄古詩(shī)文網(wǎng),

1、selenium處理圖片驗(yàn)證碼

先定位到驗(yàn)證碼圖片,在獲取驗(yàn)證碼圖片在頁(yè)面中的位置,使用save_screenshot截取頁(yè)面,再根據(jù)圖片的位置去截取驗(yàn)證碼,最后通過接口識(shí)別文字獲取驗(yàn)證碼,直接上代碼:

element = driver.find_element_by_id('imgCode') # 定位驗(yàn)證碼圖片
# 獲取驗(yàn)證碼圖片在網(wǎng)頁(yè)中的位置
left = int(element.location['x'])  # 獲取圖片左上角坐標(biāo)x
top = int(element.location['y'])  # 獲取圖片左上角y
right = int(element.location['x'] + element.size['width'])    # 獲取圖片右下角x
bottom = int(element.location['y'] + element.size['height'])  # 獲取圖片右下角y

# 通過Image處理圖像
path = current_dir + str(random.random()) + '.png'  # 生成隨機(jī)文件名
driver.save_screenshot(path)    # 截取當(dāng)前窗口并保存圖片
im = Image.open(path)        # 打開圖片
im = im.crop((left, top, right, bottom))  # 截圖驗(yàn)證碼
im.save(path)    # 保存驗(yàn)證碼圖片

# 使用百度API識(shí)別驗(yàn)證碼
def get_code():
  client = AipOcr(APP_ID, API_KEY, SECRET_KEY)  # 百度API文檔中提供的方法識(shí)別文字

  # 由于我處理的驗(yàn)證碼圖片沒有填多的線條,所以直接采用灰度是驗(yàn)證碼數(shù)字更加清晰,具體的處理方式可根據(jù)驗(yàn)證碼的實(shí)際情況而定
  im = Image.open(path)
  # 轉(zhuǎn)換為灰度圖像
  im = im.convert('L')
  im.save(path)

  # 讀取圖片,應(yīng)為百度API中提供的方法參數(shù)只能是字節(jié)流
  with open(path, 'rb')as f:
    image = f.read()
  # 使用API中提供的方法識(shí)別驗(yàn)證碼并返回驗(yàn)證碼
  code = client.basicGeneral(image)

  print(code['words_result'][0]['words']) # {'words_result': [{'words': '4TBiD ', 'location': {'top': 1, 'left': 6, 'width': 43, 'height': 13}}], 'log_id': 1358288307112378368, 'words_result_num': 1}
  return code['words_result'][0]['words']

2、使用requests請(qǐng)求驗(yàn)證碼

這里用到了會(huì)話機(jī)制,對(duì)于初學(xué)者來(lái)說(shuō)可能不太了解,簡(jiǎn)單說(shuō)一下會(huì)話機(jī)制的作用,會(huì)話就是用來(lái)保存你之前請(qǐng)求的cookie,讓瀏覽器知道你之前就在這里,這樣瀏覽器就不會(huì)認(rèn)為你重新來(lái)到這里,從而刷新驗(yàn)證碼,這樣就可以帶著我們獲取的驗(yàn)證碼去登錄了。

conn = requests.Sessoin(  # 創(chuàng)建會(huì)話
resp = conn.get('https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx')
selector = Selector(text=resp.text)
img_url = 'https://so.gushiwen.cn/'+selector.xpath('.//img[@id="imgCode"]/@src').get() # 獲取圖片的路由
img = conn.get(img_url)  # 保持會(huì)話請(qǐng)求
filename = str(random.random()) + '.png'
with open(filename, 'wb')as f:
  f.write(img.content)
# 為了后面的調(diào)用接口識(shí)別不報(bào)圖片格式錯(cuò)誤,進(jìn)行一次圖片轉(zhuǎn)換
im = Image.open(filename)
im.save(filename)
# 使用二進(jìn)制方式讀取圖片
with open(filename, 'rb')as f:
image = f.read()
data = client.handwriting(image)  # diao'yong
# 使用API中提供的方法識(shí)別驗(yàn)證碼并返回驗(yàn)證碼
code = client.basicGeneral(image)
code = code['words_result'][0]['words']

selenium源碼

# -* coding: utf-8 *-

import time
import random
from PIL import Image
from aip import AipOcr
from selenium.webdriver import Chrome

# 百度API參數(shù)
APP_ID = '23647800'
API_KEY = 'n95KOQgVuOMoAP72qZZo7uoN'
SECRET_KEY = '7yhyGglHUsY52DD8kf4w0Qjnxum07hMK'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY) # 調(diào)用API接口


def scrapy(username, password):
  """
  :param username:  用戶名
  :param password:  密碼
  """
  driver = Chrome()
  driver.get('https://so.gushiwen.cn/user/login.aspx')
  driver.find_element_by_id('email').send_keys(username) # 輸入賬號(hào)
  driver.find_element_by_id('pwd').send_keys(password)  # 輸入密碼

  element = driver.find_element_by_id('imgCode') # 定位驗(yàn)證碼圖片
  # 獲取驗(yàn)證碼圖片在網(wǎng)頁(yè)中的位置
  left = int(element.location['x']) # 獲取圖片左上角坐標(biāo)x
  top = int(element.location['y']) # 獲取圖片左上角y
  right = int(element.location['x'] + element.size['width']) # 獲取圖片右下角x
  bottom = int(element.location['y'] + element.size['height']) # 獲取圖片右下角y

  # 通過Image處理圖像
  filename = str(random.random()) + '.png' # 生成隨機(jī)文件名
  driver.save_screenshot(filename) # 截取當(dāng)前窗口并保存圖片
  im = Image.open(filename) # 打開圖片
  im = im.crop((left, top, right, bottom)) # 截圖驗(yàn)證碼
  im.save(filename) # 保存驗(yàn)證碼圖片
  # 由于我處理的驗(yàn)證碼圖片沒有填多的線條,所以直接采用灰度是驗(yàn)證碼數(shù)字更加清晰,具體的處理方式可根據(jù)驗(yàn)證碼的實(shí)際情況而定
  im = Image.open(filename)
  # 轉(zhuǎn)換為灰度圖像
  im = im.convert('L')
  im.save(filename)
  # 讀取圖片,應(yīng)為百度API中提供的方法參數(shù)只能是字節(jié)流
  with open(filename, 'rb')as f:
    image = f.read()
  # 使用API中提供的方法識(shí)別驗(yàn)證碼并返回驗(yàn)證碼
  data = client.basicGeneral(image)
  try:
    code = data['words_result'][0]['words']
  except:
    return data['error_msg']

  driver.find_element_by_id('code').send_keys(code)  # 輸入驗(yàn)證碼
  driver.find_element_by_id('denglu').click()   # 點(diǎn)擊登錄
  time.sleep(1000)  # 為了看清登錄,等待1000秒


if __name__ == '__main__':
  print(scrapy(username, password)) # 傳入你在古詩(shī)文網(wǎng)注冊(cè)的賬號(hào)密碼

requests源碼

# -* coding: utf-8 *-
import os
import random
import re
import requests
from PIL import Image
from aip import AipOcr
from scrapy import Selector

headers = {
  'referer': 'https://so.gushiwen.cn/user/login.aspx',
  'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Safari/537.36'
}

# 百度API參數(shù)
APP_ID = '23647800'
API_KEY = 'n95KOQgVuOMoAP72qZZo7uoN'
SECRET_KEY = '7yhyGglHUsY52DD8kf4w0Qjnxum07hMK'

def scrapy(username, password):
  """
  :param username:  用戶名
  :param password:  密碼
  """
  client = AipOcr(APP_ID, API_KEY, SECRET_KEY) # 調(diào)用API接口
  conn = requests.Session() # 創(chuàng)建會(huì)話
  resp = conn.get('https://so.gushiwen.cn/user/login.aspx', headers=headers) # 獲取登錄頁(yè)面
  selector = Selector(text=resp.text)
  __VIEWSTATE = selector.xpath('.//input[@id="__VIEWSTATE"]/@value').get()
  __VIEWSTATEGENERATOR = selector.xpath('.//input[@id="__VIEWSTATEGENERATOR"]/@value').get()
  img_url = 'https://so.gushiwen.cn/' + selector.xpath('.//img[@id="imgCode"]/@src').get() # 獲取圖片的路由
  img = conn.get(img_url, headers=headers) # 獲取圖片路由
  # 保存圖片
  filename = str(random.random()) + '.png' # 隨機(jī)生成文件名, 圖片格式不能為jpg,API不支持jpg格式的識(shí)別
  with open(filename, 'wb')as f:
    f.write(img.content)
   # 由于我處理的驗(yàn)證碼圖片沒有填多的線條,所以直接采用灰度是驗(yàn)證碼數(shù)字更加清晰,具體的處理方式可根據(jù)驗(yàn)證碼的實(shí)際情況而定
  im = Image.open(filename)
  # 轉(zhuǎn)換為灰度圖像
  im = im.convert('L')
  im.save(filename)
  # 使用二進(jìn)制方式讀取圖片
  with open(filename, 'rb')as f:
    image = f.read()
  # # 標(biāo)準(zhǔn)識(shí)別, 每天免費(fèi)50000次
  # data = client.basicGeneral(image)
  # 精確識(shí)別,每天免費(fèi)500次
  data = client.handwriting(image)
  # 捕獲一下接口識(shí)別當(dāng)中的錯(cuò)誤,可參照文檔查看報(bào)錯(cuò)原因
  try:
    code = data['words_result'][0]['words']
  except:
    return data['error_msg']
  form_data = {
    '__VIEWSTATE': __VIEWSTATE,
    '__VIEWSTATEGENERATOR': __VIEWSTATEGENERATOR,
    'from': '',
    'email': username,
    'pwd': password,
    'code':cod,
    'denglu': '登錄'
  }
  # 登錄
  html = conn.post('https://so.gushiwen.cn/user/login.aspx', headers=headers, data=form_data).text
  # 獲取登錄標(biāo)志位
  login_flag = re.findall("alert\('(.*?)'\);",html)[0] if re.findall("alert\('(.*?)'\);",html) else ''
  if not login_flag:
    return '登錄成功!'
  elif '驗(yàn)證碼有誤!' in login_flag:
    return "驗(yàn)證碼錯(cuò)誤"


if __name__ == '__main__':
  print(scrapy(username, password))

以上就是python爬蟲如何解決圖片驗(yàn)證碼的詳細(xì)內(nèi)容,更多關(guān)于python 解決圖片驗(yàn)證碼的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 介紹Python中的文檔測(cè)試模塊

    介紹Python中的文檔測(cè)試模塊

    這篇文章主要介紹了Python中的文檔測(cè)試模塊,代碼基于Python2.x版本,需要的朋友可以參考下
    2015-04-04
  • Python字典和列表性能之間的比較

    Python字典和列表性能之間的比較

    今天給大家介紹的是Python列表和字典的相關(guān)知識(shí),文中對(duì)Python字典和列表的性能作了充分的比較,好奇的小伙伴們一起來(lái)看看吧,需要的朋友可以參考下
    2021-06-06
  • Python中fnmatch模塊的使用詳情

    Python中fnmatch模塊的使用詳情

    這篇文章主要介紹了Python中fnmatch模塊的使用詳情,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2018-11-11
  • Python try except finally資源回收的實(shí)現(xiàn)

    Python try except finally資源回收的實(shí)現(xiàn)

    這篇文章主要介紹了Python try except finally資源回收的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Python用Jira庫(kù)來(lái)操作Jira

    Python用Jira庫(kù)來(lái)操作Jira

    這篇文章主要介紹了Python如何用Jira庫(kù)來(lái)操作Jira,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-12-12
  • 如何使用django-treebeard實(shí)現(xiàn)樹類型存儲(chǔ)與編輯

    如何使用django-treebeard實(shí)現(xiàn)樹類型存儲(chǔ)與編輯

    這篇文章主要介紹了使用django-treebeard實(shí)現(xiàn)樹類型存儲(chǔ)與編輯的宣相關(guān)操作代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2024-08-08
  • 對(duì)Python中數(shù)組的幾種使用方法總結(jié)

    對(duì)Python中數(shù)組的幾種使用方法總結(jié)

    今天小編就為大家分享一篇對(duì)Python中數(shù)組的幾種使用方法總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2018-06-06
  • Python實(shí)現(xiàn)提取或替換PPT中文本與圖片的示例代碼

    Python實(shí)現(xiàn)提取或替換PPT中文本與圖片的示例代碼

    這篇文章主要為大家詳細(xì)介紹了Python如何實(shí)現(xiàn)提取保存ppt中的圖片和替換ppt模板的文本,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-01-01
  • 如何用python編寫一個(gè)生成春聯(lián)軟件

    如何用python編寫一個(gè)生成春聯(lián)軟件

    大家好,本篇文章主要講的是如何用python編寫一個(gè)生成春聯(lián)軟件,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-01-01
  • Python使用pandas模塊實(shí)現(xiàn)表之間的關(guān)聯(lián)

    Python使用pandas模塊實(shí)現(xiàn)表之間的關(guān)聯(lián)

    在數(shù)據(jù)分析和處理中,表之間的關(guān)聯(lián)是非常常見的操作,本文為大家介紹了pandas中實(shí)現(xiàn)表之間的關(guān)聯(lián)有四種方式,感興趣的小伙伴可以了解一下
    2023-07-07

最新評(píng)論