用简单几行代码,集成百度识图(api)接口功能

前言:
想把百度识图api集成到我的某个应用中,可以官方并没有提供api接口,只能模拟访求了,就是不知道是否会有频率限制!

实践:
打开百度识图页面



打开fiddler 这里随便上传一张图片:



抓包分析发现,参数还是蛮简单的

第一步上传文件到 https://graph.baidu.com/upload    

post参数大概有:

Content-Disposition: form-data; name="image"; filename="timg.jpg"
Content-Type: image/jpeg <file>
Content-Disposition: form-data; name="tn" pc
Content-Disposition: form-data; name="from" pc
Content-Disposition: form-data; name="image_source" PC_UPLOAD_SEARCH_FILE
Content-Disposition: form-data; name="range" {"page_from": "searchResult"}

请求返回的response是个json格式的:
{"status":0,"msg":"Success","data":{"url":"https:\/\/graph.baidu.com\/s?sign=13d3d7545564e6dfa5a2801543735829&f=all&tn=pc&srcp=&tn=pc&idctag=tc&sids=10010_10125_10028_10002_10004_10105_10201_10040_10071_10062_10081_10191_10290_10390_10490_10691_10701_10704_10706_10302_10709_10801_10810_10902_9999&logid=1829298042&entrance=general&pageFrom=graph_upload_pcshitu&extUiData%5BisLogoShow%5D=1&tpl_from=pc","sign":"13d3d7545564e6dfa5a2801543735829"},"cost_nirs":30,"cost_pfc":488}

根据返回的url 进行二次请求,即可得到相应的结果

分析完毕!

程序模拟完整代码,写的比较粗糙:

#! /usr/bin/env python
#coding=utf-8
#图片来源  原图识别
#by http://tools.bugscaner.com
import requests

info = open("1.jpg","rb").read()
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36","Referer":"http://tools.bugscaner.com/"}
res = requests.Session()
data = {
    "tn":"pc",
    "from":"pc",
    "image_source":"PC_UPLOAD_SEARCH_FILE",
    "range":'''{"page_from": "searchResult"}''',
}
files = {'image':info}
response = res.post("https://graph.baidu.com/upload",data=data,files=files,verify=False,headers=headers)
returndict = response.json()
if returndict["msg"] == "Success":
    print u"上传成功,开始获取数据"
    test = res.get(returndict["data"]["url"],headers=headers,verify=False).text
    print test
else:
    print u"上传失败"


程序使用了python的强大的第三方模块,想要运行本程序,  需要安装requests

打开命令窗口输入: pip install requests   回车,即可自动安装

 


您可能还会对下面的文章感兴趣: