How to get generated captcha image using mechanize

这一生的挚爱 提交于 2019-12-03 01:15:31

This is a rough example of how to get the image, note that mechanize uses cookies so any cookies received will be sent to the server with the request for the image (this is probably what you want).

br = mechanize.Browser()
response = br.open('http://example.com')
soup = BeautifulSoup(response.get_data())
img = soup.find('img', id='id_of_image')
image_response = br.open_novisit(img['src'])
image = image_response.read()

id='id_of_image' is an example, BeautifulSoup provides many ways to find the tag you're looking for (see the BeautifulSoup docs). image_response is a file-like object.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!