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

2020-12-04  本文已影响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
上一篇 下一篇

猜你喜欢

热点阅读