爬虫:利用第三方接口实现验证码识别

断了今生、忘了曾经 提交于 2019-11-30 09:28:18

1.我的目的

  • 调用第三方接口,传输图片,获得返回(针对验证码)

2.实现过程

  • ① 选择一个第三方平台
    在这里插入图片描述
  • ② 有这么几个需求:
    • 是需要身份验证的,有两种验证方式
      在这里插入图片描述
      采用APPCODE的方式验证,可以从示例看到只需要在请求头里面加入’Authorization’参数,参数值为’APPCODE’+[APPCODE的值]
    • 需要传入一个post请求
    • 请求体中需要传入参数image,参数是url或者Base64加密的图片
      • (这里有个坑,当时整了好一会)可以看到body格式是’data:image/jpeg;base64’ + [图片base64编码];但是str是不能和base64格式直接拼接的,所以我们拼接之前要先将其编码
        post_data = {}
        with open("图片.jpg", "rb") as fp:
        temp = fp.read()
        pic = b64encode(temp)
        post_data["image"] = "data:iamge/jpeg;base64,".encode() + pic
      

3.全部代码

import requests
from base64 import b64encode
body = {}
url = "http://imgurlocr.market.alicloudapi.com/urlimages"
appcode = "e6c75f6508ba47d99d902e54ca23abbe"
headers = {
    "Authorization": "APPCODE " + appcode,
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
}
post_data = {}
with open("timg.jpg", "rb") as fp:
    temp = fp.read()
    pic = b64encode(temp)
    post_data["image"] = "data:iamge/jpeg;base64,".encode() + pic
response = requests.post(url=url, data=post_data, headers=headers)
print(response.text)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!