爬虫案例--基于selenium实现12306模拟登录

12306模拟登录:

  --使用selenium打开登录页面

  --对当前selenium打开的页面进行截图

  --对当前图片局部区域进行裁剪

    --因为如果获取图片url进行下载,相当于第二次请求图片,那么图片和打开的页面会不一致

  --使用超级鹰识别验证码图片(坐标)

 

裁剪验证码图片并保存:

# 需求:将验证码截图下载
service = Service("chromedriver.exe")
bro = webdriver.Chrome(service=service)
bro.get('https://www.taobao.com')

# 整个页面截图
bro.save_screenshot('selenium的淘宝页面截图.png')

# 定位到需要截取的标签位置
div = bro.find_elements(By.CLASS_NAME,'grid-content')[0]

# 取得标签所处的左上角坐标和标签长宽
location = div.location
size = div.size
rangle = (
   int(location['x']),int(location['y']),int(location['x']+size['width']),int(location['y']+size['height'])
   )

# 引入裁剪模块及参数:Pillow模块(from PIL import Image)
i = Image.open('./selenium的淘宝页面截图.png')
code_img_name = '聚划算div截图.png'
# 裁剪
frame = i.crop(rangle)
frame.save(code_img_name)

 

posted @ 2022-05-31 14:59  EricYJChung  阅读(79)  评论(0)    收藏  举报