Python
运行代码复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import requests
import time
import random
import pytesseract
from PIL import Image
from io import BytesIO
# 配置
captcha_url = "http://example.com/captcha.jpg"
target_url = "http://example.com/data"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3",
"Referer": "http://example.com"
}
# 代理信息
proxyHost = "www.16yun.cn"
proxyPort = "5445"
proxyUser = "16QMSOML"
proxyPass = "280651"
# 构造代理字典
proxies = {
"http": f"http://{proxyUser}:{proxyPass}@{proxyHost}:{proxyPort}",
"https": f"http://{proxyUser}:{proxyPass}@{proxyHost}:{proxyPort}"
}
def fetch_captcha():
# 使用代理请求验证码图片
response = requests.get(captcha_url, headers=headers, proxies=proxies)
image = Image.open(BytesIO(response.content))
captcha_text = pytesseract.image_to_string(image)
return captcha_text
def fetch_data_with_captcha(captcha_text):
data = {
"captcha": captcha_text
}
# 使用代理发送请求
response = requests.post(target_url, headers=headers, data=data, proxies=proxies)
return response
def main():
while True:
captcha_text = fetch_captcha()
response = fetch_data_with_captcha(captcha_text)
if response.status_code == 200:
print("数据获取成功:", response.text)
break
else:
print("验证码错误或请求失败,重新尝试...")
time.sleep(random.uniform(1, 3)) # 随机停留1到3秒
if __name__ == "__main__":
main()
若有收获,就点个赞吧