以下代码都使用python + selenium 实现,请先搭建好开发环境
1.页面的验证码若为静态验证码可以使用截屏的方式获取代码如下
driver.save_screenshot('C:/crawlerScript/picture/fetch_date.png') # 截取整个DOC
ce = driver.find_element_by_id("imgcaptcha") # 具体的id要用F12自行查看
left = ce.location['x']
top = ce.location['y']
right = ce.size['width'] + left
height = ce.size['height'] + top
im = Image.open("C:/crawlerScript/picture/fetch_date.png")
img = im.crop((left, top, right, height))
img.save('C:/crawlerScript/picture/fetch_date.png') # 这里就是截取到的验证码图片
2.若验证码为动态验证码获取方式如下:
步骤如下
1. 进入页面获取cookie
cookie = driver.get_cookies()
cookStr = ''
for i in range(0, len(cookie)):
if i == 1:
continue
cookStr += cookie[i]["name"]
cookStr += '='
cookStr += cookie[i]["value"]
cookStr += ';'
cookStr += cookie[1]["name"]
cookStr += '='
cookStr += cookie[1]["value"]
2.获取到刷新验证码的 链接
F12 定位即可
3.带上第一步的cookie去调用刷新验证码的链接
headers = {
"Cookie": cookie
}
request = requests.get('https://xxx.xxx.cn/xx/xxx/xx', headers=headers)
4.从返回中获取验证码图片
img = Image.open(BytesIO(request.content)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)