Joiike's Blog

智客日记–专注于中文的现代化和国际化

3 评论

  1. #百度翻译

    import http.client
    import hashlib #md5替换
    import urllib
    import random
    import requests
    import json
    import time

    def creaturl(yuan=”en”,mubiao=”zh”,txt=”Acrocephalus orientalis”):#接收三个字符串参数
    appid = ‘20151113000005349’
    secretKey = ‘osubCEzlGjzvw8qdQc41’
    httpClient = None
    myurl = ‘/api/trans/vip/translate’
    q = txt
    fromLang = yuan
    toLang = mubiao
    salt = random.randint(32768, 65536)
    sign = appid+q+str(salt)+secretKey
    #对签名进行加密
    m2=hashlib.md5(sign.encode(encoding=’UTF-8′)).hexdigest()
    myurl = myurl+’?appid=’+appid+’&q=’+urllib.parse.quote(q)+’&from=’+fromLang+’&to=’+toLang+’&salt=’+str(salt)+’&sign=’+m2
    return myurl
    def requ(myurl):
    try:
    re=requests.get(“https://fanyi-api.baidu.com”+myurl)
    dic=json.loads(re.text)
    src = dic[“trans_result”]
    print(src)#取得翻译后的文本结果,测试可删除注释
    for word in src:
    print(word[“src”]+’|’+word[“dst”])
    with open(“words999.txt”,”a+”) as fout:
    fout.write(word[“src”]+’|’+word[“dst”]+”\n”)

    except Exception as e:
    pass

    def main():
    with open(“words1.txt”,”r”) as f:
    links=f.readlines()
    i=0
    words=””
    for j in links:
    i+=1
    words+=j
    if i%3==0:
    #print(words)
    content=creaturl(txt=words)
    requ(content)
    words=””
    time.sleep(1.01)
    if __name__ == ‘__main__’:
    main()

发表评论

*为必填字段!