import requests def aaa(): cookies = { '__jsluid_s': 'fc5d7cb700e08b9891cdcf95030401fd', } headers = { 'Host': 'weather.121.com.cn', 'Connection': 'keep-alive', 'Pragma': 'no-cache', 'Cache-Control': 'no-cache', 'sec-ch-ua': '"Chromium";v="118", "Google Chrome";v="118", "Not=A?Brand";v="99"', 'sec-ch-ua-mobile': '?0', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36', 'sec-ch-ua-platform': '"Windows"', 'Accept': 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8', 'Sec-Fetch-Site': 'cross-site', 'Sec-Fetch-Mode': 'no-cors', 'Sec-Fetch-Dest': 'image', 'Referer': 'http://weather.sz.gov.cn/', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7', 'Cookie': '__jsluid_s=fc5d7cb700e08b9891cdcf95030401fd', 'Host': 'weather.121.com.cn', 'Connection': 'keep-alive', 'Pragma': 'no-cache', 'Cache-Control': 'no-cache', 'sec-ch-ua': '"Chromium";v="118", "Google Chrome";v="118", "Not=A?Brand";v="99"', 'sec-ch-ua-mobile': '?0', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36', 'sec-ch-ua-platform': '"Windows"', 'Accept': 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8', 'Sec-Fetch-Site': 'cross-site', 'Sec-Fetch-Mode': 'no-cors', 'Sec-Fetch-Dest': 'image', 'Referer': 'http://weather.sz.gov.cn/', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7', 'Cookie': '__jsluid_s=fc5d7cb700e08b9891cdcf95030401fd', } params = { 'source': 'pc', 'gdbsuser': '9c1bc19da29f4131bc4d3d6a26599559', 'r': '1699018207264', } response = requests.get( 'https://weather.121.com.cn/szqx/api/twt/kfr/gryy/code.do', params=params, cookies=cookies, headers=headers, ) return response.content
# encoding=utf8 # 识别验证码-GIF from PIL import Image import ddddocr # 获取GIF的各帧 def getJpg(img): im = Image.open(img) imgs = [] try: while True: current = im.tell() img = im.convert('RGB') # 可以将各个帧图片保存出来观察一下 # img_path = 'pics/' + str(current) + '.jpg' # img.save(img_path) # 将获取的图片放到列表里面,给后面合成图片用 imgs.append(img) im.seek(current + 1) except: pass return imgs # 多张图片合成一张 def conflate(img_paths): cage = [] # 笼子,把图片放进来合并,如果有两张就合并,一笼不容二虎 num = 0 for img in img_paths: num += 1 cage.append(img) if len(cage) == 2: merge = Image.blend(cage[0], cage[1], 0.5) # 合并两张图片,透明度0.5 cage = [merge] # 合并完,重置笼子 # 把合成完的图片保存出来,只是为了看看结果,后续直接用merge识别就行了 merge.save("cage/intact.jpg") return merge #识别验证码x def ivd(img): ocr = ddddocr.DdddOcr() res = ocr.classification(img) return res if __name__ == '__main__': from xxx import aaa from io import BytesIO imgs = getJpg(BytesIO(aaa())) merge = conflate(imgs) res = ivd(merge) print(res)