美文网首页
python免费调用爱词霸翻译接口

python免费调用爱词霸翻译接口

作者: 是东东 | 来源:发表于2020-12-04 10:35 被阅读0次

text:文本限制5000字符。

    def translate(self, text):
        """翻译"""
        headers = {'X-Forwarded-For': '', 'User-Agent': (UserAgent()).random}
        data = {"f": "auto", "t": "auto", "w": text}
        url = "http://fy.iciba.com/ajax.php?a=fy"
        response = s.post(url, headers=headers, data=data, timeout=15).text
        obj = json.loads(response)
        time.sleep(1)
        status = obj.get('status')
        content = obj.get('content')
        if status == 1:
            out = content.get('out')
            return out
        else:
            word_mean, out = "".join(content.get("word_mean")), ''
            mean = re.findall(r";(.*?);", word_mean)
            if not mean:
                mean = re.findall(r"(.*?);", word_mean)
            if mean:
                out = mean[0]
        return out

相关文章

网友评论

      本文标题:python免费调用爱词霸翻译接口

      本文链接:https://www.haomeiwen.com/subject/ajvbwktx.html