擅长:python、mysql、java
<p>下面是一个完整的例子(以google的recaptcha为目标):</p>
<pre><code>import urllib
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://www.google.com/recaptcha/demo/recaptcha')
# get the image source
img = driver.find_element_by_xpath('//div[@id="recaptcha_image"]/img')
src = img.get_attribute('src')
# download the image
urllib.urlretrieve(src, "captcha.png")
driver.close()
</code></pre>
<p>更新:</p>
<p>动态生成图像的问题是,每次请求时都会生成一个新图像。在这种情况下,您有几个选项:</p>
<ul>
<li><p>截图</p>
<pre><code>from selenium import webdriver
driver = webdriver.Firefox()
driver.get('https://moscowsg.megafon.ru/ps/scc/php/cryptographp.php?PHPSESSID=mfc540jkbeme81qjvh5t0v0bnjdr7oc6&ref=114&w=150')
driver.save_screenshot("screenshot.png")
driver.close()
</code></pre></li>
<li><p>模拟右击+另存为。有关详细信息,请参见<a href="https://stackoverflow.com/questions/433919/javascript-simulate-right-click-through-code">this thread</a>。</p></li>
</ul>
<p>希望能有所帮助。</p>