How can I retrieve image captcha from a webpage using Python?

In Python, obtaining a webpage image captcha requires the use of the third-party libraries requests and PIL (Python Imaging Library).

Firstly, you can use the requests library to send a GET request to retrieve the captcha image from the website. You can use the following code:

import requests

url = 'http://example.com/captcha.jpg'  # 验证码图片的URL
response = requests.get(url)

with open('captcha.jpg', 'wb') as f:
    f.write(response.content)

In the above code, first specify the URL of the captcha image, then use the requests library to send a GET request to retrieve the content of the captcha image, and finally write the content to a local file named captcha.jpg.

Next, open and display the captcha image using the PIL library, you can use the following code:

from PIL import Image

image = Image.open('captcha.jpg')
image.show()

The code above first opens the local captcha image “captcha.jpg” using the Image module of the PIL library, then displays the captcha image using the show() method.

You can process the verification code image according to specific needs, such as cropping, grayscaling, binarization, etc., for subsequent verification code recognition.

bannerAds