selenium(截取图片)

# PIL属于Pillow模块下的一个模块,主要是用来处理图片。
from PIL import Image
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
import requests

# 使用selenium进行验证码图片的截图:
# 1. 将整个页面截取一个大图;
# 2. 在页面上通过xpath/cssselect定位到验证码图片;
# 3. 获取验证码图片在整个大图中的坐标;
# 4. 根据坐标从大图中截取小图;

driver = webdriver.Firefox()

driver.get('http://www.yundama.com/')

image = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_id('verifyImg'))

# 截取大图
driver.save_screenshot('page.png')

# 确定图片距离页面上部和左侧的大小
left = image.location['x']
top = image.location['y']
# 确定图片距离页面底部和右侧的大小
right = left + image.size['width']
bottom = top + image.size['height']

# 截图小图
img = Image.open('page.png')
img = img.crop((left, top, right, bottom))

img.save('captcha.png')


# 单独发送请求获取验证码
res = requests.get('http://www.yundama.com/index/captcha').content
with open('my.png', 'wb') as f:
    f.write(res)

亲测有效(奸笑脸哈哈哈...)

猜你喜欢

转载自blog.csdn.net/qq_42336549/article/details/80867690
今日推荐