[问题] 爬虫问题

楼主: boran0903 (浪子)   2018-01-28 04:10:28
大家好,
新手发问,我尝试写一只爬虫来帮忙工作上使用,但在该网页要撷取图像认证似乎座标都抓取错误,可否请版上有经验的高手帮忙解答 谢谢!!
以下为我的程式码,我尝试在抓到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')
作者: vi000246 (Vi)   2018-01-28 11:00:00
不要用座标 直接把图片抓下来

Links booklink

Contact Us: admin [ a t ] ucptt.com