今天突然想要做一个百度翻译的接口,但是发现百度是有提供免费的接口的,但是限制翻译的字数,最主要的还是我懒得注册,所以我们来简单的实现百度的翻译接口。
Ⅰ.实现原理
我们在小学二年级的时候就听说过,百度翻译的正常接口,是有通过token加密的,还比较难破解。但是通过观察,百度是存在另外一个实时翻译的接口的,就是当我们每次输入东西的时候都会在一个接口上访问。

每次在我们输入一个字符都会往这个sug接口访问数据,并且这个接口竟然只需要接收一个参数:kw 即可,完全不需要验证什么token!所以我们就从这个下手!
Ⅱ.Python实现
首先我们需要引入一个超级好用的python库:
import requests如果显示没有这个库,那么请先下载这个库:
# 在控制台输入以下代码
# 如果显示不存在 pip 这个命令证明你的pip环境变量未配置
pip install requests接下来,按照平常的思路,提供headers和data即可完成
如果你返回的结构不是中文而是一堆/u开头的16进制数
那么将获得的结构进行转码即可获得中文字符:
结果.encode('utf-8').decode('unicode_escape')
下面直接提供完整代码:
import requests
url = "https://fanyi.baidu.com/sug"
headers = {
'Origin': 'https://fanyi.baidu.com',
'Referer': 'https://fanyi.baidu.com/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.81 Safari/537.36 Edg/104.0.1293.54',
}
def main():
value = '你好' # 输入你想要翻译的内容
data = {'kw' : value}
req = requests.post(url=url, headers=headers, data=data)
print(req.text.encode('utf-8').decode('unicode_escape'))
if __name__ == '__main__':
main()测试结果:

Ⅲ.局限性
你以为你钻到了空子,实际上这只是百度免费提供给你的小接口,因为这个sug接口只能识别一个完整词组、单词、成语,如果你输入一个长句子甚至只要超过1个单词他就给你罢工

因为2个或两个以上的词组是交给另外一个langdetect接口实现的,原理也是需要通过token来验证的,所以,这个接口只能够实现单个词组的翻译。但是,我觉得也足够啦!并且换个思路,这个接口还能提供短语验证功能呢!如果真想实现长句翻译,还是老老实实去注册个百度翻译接口账号吧,免费版的还是能翻译非常多字的!
四.在自己网页上实现接口翻译
有了原理,我们希望通过访问自己的网站实现调用自己网站接口进行百度翻译,其实也是非常简单的,大概的原理只需要在自己的网页上接收post请求,再对百度接口端获取数据再返回,实现代码:
<?php
function send_post($url, $post_data) {
$header[] = 'Origin:https://fanyi.baidu.com';
$header[] = 'Referer:https://fanyi.baidu.com/';
$header[] = 'User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.81 Safari/537.36 Edg/104.0.1293.54';
$postdata = http_build_query($post_data);
$options = array(
'http' => array(
'method' => 'POST',
'header' => $header,
'content' => $postdata,
'timeout' => 15 * 60 // 超时时间(单位:s)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
function server_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data, ENT_QUOTES);
return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (!empty($_POST["value"])) {
$post_data = array(
'kw' => server_input($_POST["value"]),
);
echo send_post('https://fanyi.baidu.com/sug', $post_data);
} else {
echo 'error: key word is empty!';
}
}
?>如此一来,只需要在其他程序上(这里还是以Python为例)对你网站的如上php代码进行post请求(请求格式可以自己设计,比如自己加些token来验证是不是本人操作等,这个演示代码是只有一个值 value : 翻译内容)
python请求代码:
import requests
url = '你网站的接口地址(就是上面的php代码地址)'
data = {
'value' : r'你好', # r 表示的是仅字符串 (使得 \ 的转义字符失效)
}
req = requests.post(url=url, data=data)
print(req.text.encode('utf-8').decode('unicode_escape'))Ⅴ.小结
以上就是百度翻译接口的实现啦,其实不一定是百度翻译,有道翻译也是可以实现的,并且还可以实现长段的直接翻译,感兴趣的小伙伴可以自行上网搜实现方法呀,方法总比困难多,希望大家也可以自行研究百度翻译接口的破解方法呀!




Comments | 10 条评论
Nice post. I learn something more challenging on different blogs everyday. It will always be stimulating to read content from other writers and practice a little something from their store. I’d prefer to use some with the content on my blog whether you don’t mind. Natually I’ll give you a link on your web blog. Thanks for sharing.
Эта статья предлагает уникальную подборку занимательных фактов и необычных историй, которые вы, возможно, не знали. Мы постараемся вдохновить ваше воображение и разнообразить ваш кругозор, погружая вас в мир, полный интересных открытий. Читайте и открывайте для себя новое!
Получить дополнительные сведения – https://vivod-iz-zapoya-1.ru/
Hey there! Do you know if they make any plugins to protect against hackers? I’m kinda paranoid about losing everything I’ve worked hard on. Any recommendations?
I truly enjoy looking at on this internet site, it contains excellent content.
An attention-grabbing dialogue is value comment. I believe that you should write more on this matter, it might not be a taboo subject but typically individuals are not sufficient to speak on such topics. To the next. Cheers
I’m not sure where you’re getting your info, but good topic. I needs to spend some time learning more or understanding more. Thanks for fantastic information I was looking for this information for my mission.
Hello my friend! I want to say that this post is awesome, nice written and come with approximately all important infos. I would like to peer more posts like this .
Normally I do not read article on blogs, however I would like to say that this write-up very forced me to check out and do so! Your writing style has been surprised me. Thank you, quite great article.
I like what you guys are up too. Such intelligent work and reporting! Carry on the excellent works guys I have incorporated you guys to my blogroll. I think it’ll improve the value of my website :)
I went over this site and I conceive you have a lot of excellent info , saved to fav (:.