所谓市面上的智能ai伪原创工具,python百度翻译

python“智能AI伪原创”,需求模块“js2py”

#!/usr/bin/env python
# -*- coding:utf-8 -*-
 
'''
爬虫之百度翻译
需要的库有 js2py, requests, re, json
'''
 
__author__ = 'YXQ'
 
 
import js2py
import requests
import json
import re
import sys
reload(sys)
sys.setdefaultencoding('utf-8')


#百度翻译的主页
url_fanyi = 'http://fanyi.baidu.com'
#翻译时post的api
url_api = 'http://fanyi.baidu.com/v2transapi'

def get_fanyi(bdfro,bdto,query):

    #设置 headers
    headers = {
        'Cookie':'BAIDUID=6AB7BF38A9055361B9E0EA56481C75AF:FG=1; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; PSTM=1564708619; BIDUPSID=81E65A350C15F58C5FCA79FD908B83E6; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; SIGNIN_UC=70a2711cf1d3d9b1a82d2f87d633bd8a03162947566; to_lang_often=%5B%7B%22value%22%3A%22en%22%2C%22text%22%3A%22%u82F1%u8BED%22%7D%2C%7B%22value%22%3A%22zh%22%2C%22text%22%3A%22%u4E2D%u6587%22%7D%5D; BDUSS=3AzQVo4RUtZdFVJUElMV2RKQjRkdn5udDFsazZ1N0FHcGxYbGVma3VJbU52NFZkSVFBQUFBJCQAAAAAAAAAAAEAAAAFf9i-MjM0NWR1b3RlAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAI0yXl2NMl5db; bdindexid=7955dc29nf7n59mfb6fl41ogr3; BDSFRCVID=TlLOJeC626IhMcOwVP9ItWUsqeaMCwQTH6aoShHmHOyjJph-pRD_EG0PeM8g0Ku-HZsCogKK0eOTHkCF_2uxOjjg8UtVJeC6EG0P3J; H_BDCLCKID_SF=tJkD_I_hJKt3qn7I5KToh4Athxob2bbXHDo-LIvj5MJcOR5JhfA-3R-e046fQMJCMjnU-4Oh5lvvhb3O3M7ShbKq-4bBJJoELH7qBMQF5l8-sq0x0bOte-bQypoa0q3TLDOMahkM5h7xOKQoQlPK5JkgMx6MqpQJQeQ-5KQN3KJmhpFuD6AMjjjXDN8s-tcjfPo8Q4j_5-F_HnurBUOOXUI8LNDHtb5MBGR9LMOs-p7l8nrsXqj1WJ8dXnO7ttoAtD8qaxc5QJ_KJMc10UTG5ML1Db37L6vMtg3t3tJmtJroepvoD-oc3MkBytjdJJQOBKQB0KnGbUQkeq8CQft205tpeGKfqTKetb4DV-35b5rjjb7kbn3hq4tHen3v3xRZ5mAqotobJhr-D43b544b3f4HX-c-XnQX-TrnaIQqQtbmfqrx567DBUAl3brG5fQ43bRTapCy5KJvfJo2jfuahP-UyN3-Wh37be5lMKoaMp78jR093JO4y4Ldj4oxJp8eWJLHoCIytD-BhCvPbJOHq4FQ-Uvy-40fHD7yWCkXWpvcOR5J5UchQfkFDf6fQ5oafTnU-4OD5KoHMMcC3MA--t4DMt7tLUDebNTDKKDKMxPasq0x0-ole-bQypoaJpjm3KOMahkM5l7xObvJQlPK5JkgMx6MqpQJQeQ-5KQN3KJmhpFuDjtBDT30DGR22Pc22KAX3RbEK4OMet_k-PnVq4tHeUttWnJZ5m7mXp0b2qbVH43Kjb5H2qQB2R3Gt5LJ3GAeLC520COkbC_RD6tMjjQM5pJfetoj-IJ-sJOOaCvxDhROy4oTj6jByf7u5MRfQH48a4b6Q4OaeJjELnJG3MvB-fngaUvLWgO8BqcM2T62Vlb3Qft20-LIeMtjBbLLWNQe5b7jWhk2eq72y-RTQlRX5q79atTMfNTJ-qcH0KQpsIJMDUC0-nDSHH-tt6-83j; from_lang_often=%5B%7B%22value%22%3A%22zh%22%2C%22text%22%3A%22%u4E2D%u6587%22%7D%2C%7B%22value%22%3A%22it%22%2C%22text%22%3A%22%u610F%u5927%u5229%u8BED%22%7D%2C%7B%22value%22%3A%22en%22%2C%22text%22%3A%22%u82F1%u8BED%22%7D%5D; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1565849288,1566199360,1566532260,1566536736; Hm_lvt_afd111fa62852d1f37001d1f980b6800=1566536884; H_WISE_SIDS=132694_125703_127759_128699_134696_134862_128068_133679_120215_135321_132909_131246_132440_130762_132378_131518_118879_118861_118848_118832_118805_107320_133351_132553_129653_132250_127024_128967_135347_133473_133838_133847_132551_134460_134319_134213_129644_131423_134600_134488_134496_110085_135221_134152_127969_131951_127316_127416_134151_132467_134653_134353; delPer=0; rsv_i=1db4w84nW%2BcmUPF70180BKcysxF6iOQUzyEvya4WSz6bRm5usW%2Fw31hJSiKt8%2BXho1%2Fsz4ULsKB9GNWisQ7NF3fz%2FTzWRDI; FEED_SIDS=543483_0823_12; BDPASSGATE=IlPT2AEptyoA_yiU4VK03kIN8efBSrK41hHESlpt3FOdfCaWmhH3BrVMSEnHN-a8AiTM-Y3fo_-5kV8PQlxmicwdggYTpW-H7CG-zNSF5aTvL_NgzbIZCb4jQUZqqvzxkgl-zuEHQ49zBCstpBTbpuo4ivKl73JQb4r56kCygKrl_oGm2X8My88bOXVfYZ0APNu594rXnEpKLDm4YxOtT9PecSIMR7QEy0amdqslO4ftuTsyHPbSFfA10YjdGXV3Ef_-3OmXzKio1-IvvnlfVjw-d_; SE_LAUNCH=5%3A26108994; locale=zh; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1566539863; Hm_lpvt_afd111fa62852d1f37001d1f980b6800=1566539863; yjs_js_security_passport=11534d5839c04ac5978416777d86416942080725_1566539866_js',
        'User-Agent':'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Mobile Safari/537.36',
    }
     
    #get百度翻译主页,是为了得到token
    req_get = requests.get(url = url_fanyi, headers = headers)
    token = re.search(r"token: '(.*?)',", req_get.text, re.S).group(1)
     
    #需要翻译的内容,中英文都可以
    translation_content = query
    translation_content = translation_content.decode('utf8')
     
    #读入js代码
    def get_js():
        with open('sign.js') as f:
            return f.read()
    #使用js2py在python中运行js代码并得到sign
    run_js = js2py.EvalJs({})
    run_js.execute(get_js())
    sign = run_js.e(translation_content)

    url = 'https://fanyi.baidu.com/basetrans'
    data = {
            "from":bdfro,
            "to":bdto,
            "query":query,
            "sign":sign,
            "token":"607bb9dba8d4a9f2ef74913475a5e1ed",
        }


    r = requests.post(url,data=data,headers=headers)

    result = json.loads(r.text)
    return result['trans'][0]['dst']

if __name__ == '__main__':
    
    with open('content.txt') as contents:
        for content in contents:
            content = content.strip()
            zhjson = get_fanyi('zh','en',content)
            enjson = get_fanyi('en','zh',zhjson.encode('utf8'))

            print enjson

此处JS代码另存为文件"sign.js"

function n(r, o) {
    for (var t = 0; t < o.length - 2; t += 3) {
        var a = o.charAt(t + 2);
        a = a >= "a" ? a.charCodeAt(0) - 87 : Number(a),
        a = "+" === o.charAt(t + 1) ? r >>> a: r << a,
        r = "+" === o.charAt(t) ? r + a & 4294967295 : r ^ a
    }
    return r
}
function e(r) {
    var o = r.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g);
    if (null === o) {
        var t = r.length;
        t > 30 && (r = "" + r.substr(0, 10) + r.substr(Math.floor(t / 2) - 5, 10) + r.substr( - 10, 10))
    } else {
        for (var e = r.split(/[\uD800-\uDBFF][\uDC00-\uDFFF]/), C = 0, h = e.length, f = []; h > C; C++)"" !== e[C] && f.push.apply(f, a(e[C].split(""))),
        C !== h - 1 && f.push(o[C]);
        var g = f.length;
        g > 30 && (r = f.slice(0, 10).join("") + f.slice(Math.floor(g / 2) - 5, Math.floor(g / 2) + 5).join("") + f.slice( - 10).join(""))
    }
    var u = void 0, i = null;
    u = null !== i ? i: (i = "320305.131321201" || "") || "";
    for (var d = u.split("."), m = Number(d[0]) || 0, s = Number(d[1]) || 0, S = [], c = 0, v = 0; v < r.length; v++) {
        var A = r.charCodeAt(v);
        128 > A ? S[c++] = A: (2048 > A ? S[c++] = A >> 6 | 192 : (55296 === (64512 & A) && v + 1 < r.length && 56320 === (64512 & r.charCodeAt(v + 1)) ? (A = 65536 + ((1023 & A) << 10) + (1023 & r.charCodeAt(++v)), S[c++] = A >> 18 | 240, S[c++] = A >> 12 & 63 | 128) : S[c++] = A >> 12 | 224, S[c++] = A >> 6 & 63 | 128), S[c++] = 63 & A | 128)
    }
    for (var p = m,
    F = "+-a^+6",
    D = "+-3^+b+-f",
    b = 0; b < S.length; b++) p += S[b],
    p = n(p, F);
    return p = n(p, D),
    p ^= s,
    0 > p && (p = (2147483647 & p) + 2147483648),
    p %= 1e6,
    p.toString() + "." + (p ^ m)
}


本文由站长原创或收集,不代表本站立场,如若转载,请注明出处:https://pds.ac.cn/seo/12.html

本文 暂无 评论

回复给

欢迎点评

联系我们

站长QQ:770083771

站长邮件:770083771@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

QR code