大家好,
新手發問,我嘗試寫一隻爬蟲來幫忙工作上使用,但在該網頁要擷取圖像認證似乎座標都抓取錯誤,可否請版上有經驗的高手幫忙解答 謝謝!!
以下為我的程式碼,我嘗試在抓到X,Y座標印出字體,但座標似乎離數字驗證的座標點有差異.
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from pandas.compat import StringIO
from PIL import Image
import time
import os
#browser = webdriver.Firefox()
#browser = webdriver.Ie('IEDriverServer.exe')
browser = webdriver.Chrome('C:\geckodriver\chromedriver.exe')
browser.get('http://mrtg.chief.com.tw/login.php')
#browser.set_window_position(0,0) #瀏覽器位置
#browser.set_window_size(700,700) #瀏覽器大小
browser.save_screenshot("c:\\test\\test.png")
inputUsername = browser.find_element_by_name("login_id")
inputPassword = browser.find_element_by_name("password")
png = browser.find_element_by_xpath("//*[contains(@name=, 'imgcode')]")
#png = browser.find_element_by_id("checkword")
loc,size = png.location,png.size
left,top = loc['x'],loc['y']
width,height = size['width'],size['height']
box=(int(left),int(top),int(left+width),int(top+height))
print (png.location)
print (left,top)
from PIL import Image,ImageDraw,ImageFont
img = Image.open("c:\\test\\test.png")
ttfont=ImageFont.truetype("C:\\Windows\\Fonts\\georgia.ttf",50)
draw=ImageDraw.Draw(img)
draw.text((left,top),"Good",font=ttfont,fill=(255,0,0,255))
img.show()
img = Image.open("c:\\test\\test.png")
img.save('c:\\test\\mysave.jpg','jpeg')
img = img.crop(box)
img.show()
img.save('c:\\test\\captua.jpg','jpeg')
# 搜尋框輸入字
inputUsername.send_keys(u'test')
inputPassword.send_keys(u'test')