1

我正在尝试下载每次加载页面时其 URL 和内容都会动态变化的验证码图片,我知道我可以为浏览器截屏并找到验证码图片的位置,但我无法找到验证码图片。

从 HTML 源代码中我发现了这个

//此脚本用于生成验证码

<iframe marginheight="0" marginwidth="0" scrolling="no" frameborder="0" width="203" height="53" name="Captcha" src="/efs/servlet/efs/jsp-ns/captcha.jsp"></iframe>

//当我点击 src="/efs/servlet/efs/jsp-ns/captcha.jsp" 时,它把我带到了这个

<html>
<head><meta scheme='a1afcc517bec909bf5c3fddea7c83c3d' name='TSd58639' content='b133d7457db43c81' /> <meta scheme='eb1e31097f37b3d64bef23cbd5cab231' name='1000' content='5' /><!-- 9cc5da25f89a21d1fbb5ffa18da0bb73 --><script type="text/javascript">//<![CDATA[
eval(function(a){var f=a.split("");var c=f.length;var b=parseInt(f[0]+f[1],16);var e=String.fromCharCode(b);for(var d=2;d<c;d++){var g=(parseInt(f[d]+f[d+1],16)-b)%256;b=g;e+=String.fromCharCode(g);d++}return e}("288..."));
</script>
<script language="JavaScript">var pn = "CSRT"; var pv = '3642466061891909727';
eval(function(a){var f=a.split("");var d=f.length;var c=parseInt(f[0]+f[1],16);var e=String.fromCharCode(c);for(var b=2;b<d;b++){var g=(parseInt(f[b]+f[b+1],16)-c)%256;c=g;e+=String.fromCharCode(g);b++}return e}("288edbe3..."));
</script>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<img src="Captcha.jpg?t=1378993130057" border=1/>
</body>
</html>

此行'<img src="Captcha.jpg?t=1378993130057" border=1/>'定义验证码 url 但数字 't=1378993130057' 动态变化

我已经看到这个线程 使用 selenium python 下载图像, 但我不明白作者如何找到图像位置,例如

img = browser.find_element_by_xpath('//*[@id="cryptogram"]')

对于谷歌验证码 [ http://www.google.com/recaptcha/demo/recaptcha]

img = driver.find_element_by_xpath('//div[@id="recaptcha_image"]/img')

python 2.6 我正在使用 Selenuim 浏览网站

更新

try:
    browser.save_screenshot('screenshot.png')
    img = browser.find_element_by_xpath('//body/img')
    src = img.get_attribute('src')
    loc = img.location

except Exception,e:
    print e

输出

Message: u'Unable to locate element: {"method":"xpath","selector":"//body/img"}' ; Stacktrace: 
    at FirefoxDriver.prototype.findElementInternal_ (file:///tmp/tmppjlmPW/extensions/fxdriver@googlecode.com/components/driver_component.js:8899)
    at FirefoxDriver.prototype.findElement (file:///tmp/tmppjlmPW/extensions/fxdriver@googlecode.com/components/driver_component.js:8908)
    at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmppjlmPW/extensions/fxdriver@googlecode.com/components/command_processor.js:10840)
    at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmppjlmPW/extensions/fxdriver@googlecode.com/components/command_processor.js:10845)
    at DelayedCommand.prototype.execute/< (file:///tmp/tmppjlmPW/extensions/fxdriver@googlecode.com/components/command_processor.js:10787)

更新#2

from selenium import webdriver
import datetime
from selenium.webdriver.common.proxy import *


print '[+] Starts at '+ datetime.datetime.now().isoformat()

browser = webdriver.Firefox() 
browser.get("https://www.example.com") 


try:
    browser.save_screenshot('screenshot.png')
    img = browser.find_element_by_xpath('//body/img')
    src = img.get_attribute('src')
    loc = img.location

except Exception,e:
    print e


browser.delete_all_cookies()
browser.close()

print '[+] Done at ' + datetime.datetime.now().isoformat()

任何帮助深表感谢。

4

2 回答 2

1

您可以img通过 xpath 获取标签,获取src属性值,然后通过以下方式下载urlretrieve

import urllib

img = browser.find_element_by_xpath('//body/img')
src = img.get_attribute("src")
urllib.urlretrieve(src, "captcha.png")
于 2013-09-12T13:51:05.820 回答
0
import urllib

img = browser.find_element_by_cssselector("img[src*='Captcha.jpg']")

src = img.get_attribute("src")

urllib.urlretrieve(src, "Captcha.jpg")

试试这个方法,让我知道它是否有效。

于 2014-04-09T08:47:06.990 回答