spider pc版百度翻譯介面 post方式

2021-08-21 12:14:55 字數 1760 閱讀 3009

分析過程,略……,有時間再補

python**:

#!/usr/bin/env python

# -*- coding:utf-8 -*-

'''需要的庫有 js2py, requests, re, json

'''__author__ = 'yxq'

import js2py

import requests

import json

import re

url_fanyi = ''

#翻譯時post的api

url_api = '/v2transapi'

#設定 headers

headers =

req_get = requests.get(url = url_fanyi, headers = headers)

token = re.search(r"token: '(.*?)',", req_get.text, re.s).group(1)

#需要翻譯的內容,中英文都可以

translation_content = '我是乙個中國人,我不為五斗公尺折腰'

#讀入js**

def get_js():

with open('sign.js', 'r', encoding='utf-8') as f:

return f.read()

#使用js2py在python中執行js**並得到sign

run_js = js2py.evaljs({})

run_js.execute(get_js())

sign = run_js.e(translation_content)

data =

req_post = requests.post(url = url_api, data = data, headers = headers)

result = json.loads(req_post.text)

print(result['trans_result']['data'][0]['dst'])

js**:

function n(r, o) 

return r

}function e(r) else

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++)

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)

}

將js**儲存為sign.js並放在python檔案同目錄

百度翻譯介面

public class translateacy extends activity implements onclicklistener public void initview private handler inshandler new handler 翻譯 private void tran...

百度翻譯介面例項解析

本例項採用python3進行編寫 獲取 token 和 gtk 如下 請求頭非常重要,在請求 fanyi.baidu.com 這個頁面的時候需要傳遞 header 獲取網頁原始碼 html requests.get headers header html.encoding utf 8 正則匹配 gt...

百度翻譯 介面使用例項

獲取目標url所列印的內容 static function language text url else return file contents 定義需要翻譯的內容 title 你好 驗證是否為漢字 相容gb2312,utf 8 if preg match x7f xff title else 結...