{"id":160704,"date":"2019-10-23T09:13:09","date_gmt":"2019-10-23T01:13:09","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=160704"},"modified":"2019-10-07T18:13:56","modified_gmt":"2019-10-07T10:13:56","slug":"python-translation-tool","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-translation-tool.html","title":{"rendered":"\u5229\u7528Python\u5b9e\u73b0\u7ffb\u8bd1"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5229\u7528Requests\u6a21\u5757\u83b7\u53d6\u6709\u9053\u8bcd\u5178web\u9875\u9762\u7684post\u4fe1\u606f\uff0cBeautifulSoup\u6765\u83b7\u53d6\u9700\u8981\u7684\u5185\u5bb9\uff0c\u901a\u8fc7tkinter\u6a21\u5757\u751f\u6210gui\u754c\u9762\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u4e00\u3001\u4ee3\u7801<\/strong><\/div>\n

fanyi.py\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

#!\/bin\/env python\r\n# -*- coding:utf-8 -*-\r\n# _author:kaliarch\r\nimport requests\r\nimport urllib.parse\r\nimport time\r\nimport random\r\nimport hashlib\r\nimport json\r\n\r\nclass search(object):\r\n    def __init__(self):\r\n        self.url = 'http:\/\/fanyi.youdao.com\/translate_o?smartresult=dict&smartresult=rule'\r\n\r\n    def getData(self,search_name):\r\n        # salt =i = \"\" + ((new Date).getTime() + parseInt(10 * Math.random(), 10)\r\n        salt = ((time.time() * 1000) + random.randint(1,10))\r\n        # sign = n.md5(\"fanyideskweb\" + t + i + \"ebSeFb%=XZ%T[KZ)c(sy!\")\r\n        sign_text = \"fanyideskweb\" + search_name + str(salt) + \"ebSeFb%=XZ%T[KZ)c(sy!\"\r\n        sign = hashlib.md5((sign_text.encode('utf-8'))).hexdigest()\r\n        paydata = {\r\n            'i': search_name,\r\n            'from': 'AUTO',\r\n            'to': 'AUTO',\r\n            'smartresult': 'dict',\r\n            'client': 'fanyideskweb',\r\n            'salt': salt,\r\n            'sign': sign,\r\n            'doctype': 'json',\r\n            'version': '2.1',\r\n            'keyfrom': 'fanyi.web',\r\n            'action': 'FY_BY_CLICKBUTTION',\r\n            'typoResult': 'false'\r\n        }\r\n        return paydata\r\n\r\n    def getHeader(self):\r\n        header = {\r\n            'Host': 'fanyi.youdao.com',\r\n            'Referer': 'http:\/\/fanyi.youdao.com\/',\r\n            'Content-Type': 'application\/x-www-form-urlencoded; charset=UTF-8',\r\n            'User-Agent': 'Mozilla\/5.0 (Windows NT 10.0; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/55.0.2883.87 Safari\/537.36',\r\n            'Cookie': 'OUTFOX_SEARCH_USER_ID=-846616837@1.80.219.201; OUTFOX_SEARCH_USER_ID_NCOO=129549097.60835753; UM_distinctid=15ff309f18ddc-094cb5494ad815-5d4e211f-1fa400-15ff309f18e449; _ga=GA1.2.184261795.1517119351; __guid=204659719.2556877880764680700.1518435624954.942; JSESSIONID=aaa3A5BLhtTrh4TPX_mgw; monitor_count=2; ___rl__test__cookies=1518488731567'\r\n        }\r\n        return header\r\n\r\n    def getRequest(self,paydata,header):\r\n        _data = urllib.parse.urlencode(paydata).encode('utf-8')\r\n        _header = header\r\n        response = requests.post(self.url,data=_data,headers=_header)\r\n        return response.text\r\n\r\n    def getResult(self,response):\r\n        result_text = json.loads(response)\r\n        #src = result_text['translateResult'][0][0]['src']\r\n        tgt = result_text['translateResult'][0][0]['tgt']\r\n        return tgt\r\n\r\n    def main(self,search_name):\r\n        app = search()\r\n        paydata = app.getData(search_name)\r\n        header = app.getHeader()\r\n        response = app.getRequest(paydata, header)\r\n        tgt = app.getResult(response)\r\n        return tgt\r\n<\/pre>\n

windows.py\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

#!\/bin\/env python\r\n# -*- coding:utf-8 -*-\r\n# _author:kaliarch\r\n\r\nimport tkinter as tk\r\nfrom fanyi import search\r\n\r\nclass application:\r\n    def __init__(self):\r\n        self.windows = tk.Tk()\r\n        self.windows.title(\"\u7ffb\u8bd1\u5c0f\u5de5\u5177\")\r\n        self.windows.geometry(\"280x350+700+300\")\r\n\r\n        #\u63d0\u4ea4\u6309\u94ae\r\n        self.submit_btn = tk.Button(self.windows, text=\"\u67e5\u8be2\",command=self.submit)\r\n        self.submit_btn.place(x=220, y=10, width=50, height=25)\r\n\r\n        # \u5b9a\u4e49\u8f93\u5165\u6846\r\n        self.entry = tk.Entry(self.windows)\r\n        self.entry.place(x=10, y=10, width=200, height=40)\r\n\r\n        #\u8f93\u51fa\u5185\u5bb9\r\n        self.result_text = tk.Text(self.windows, background=\"#ccc\")\r\n        self.result_text.place(x=10, y=90, width=260, height=245)\r\n\r\n        # \u7ffb\u8bd1\u7ed3\u679c\u6807\u9898\r\n        self.title_label = tk.Label(self.windows, text=\"\u7ffb\u8bd1\u7ed3\u679c\uff1a\")\r\n        self.title_label.place(x=10, y=65)\r\n        self.search_result = search()\r\n\r\n    def submit(self):\r\n        #1.\u83b7\u53d6\u7528\u6237\u8f93\u5165\r\n        context = self.entry.get()\r\n\r\n        #2.\u5229\u7528\u6709\u9053\u7ffb\u8bd1\r\n        result = self.search_result.main(context)\r\n        #3.\u8f93\u51fa\r\n        self.result_text.delete(1.0,tk.END)\r\n        self.result_text.insert(tk.END,result)\r\n\r\n    def run(self):\r\n        self.windows.mainloop()\r\n\r\nif __name__ == '__main__':\r\n    winapp = application()\r\n    winapp.run()\r\n<\/pre>\n

setup.py\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

# -*- coding:utf-8 -*-\r\n# _author:kaliarch\r\n\r\nimport sys\r\nfrom cx_Freeze import setup,Executable\r\n\r\nimport os\r\n\r\nos.environ['TCL_LIBRARY'] = r\"C:\\Program Files\\Python36\\tcl\\tcl8.6\"\r\nos.environ['TK_LIBRARY'] = r\"C:\\Program Files\\Python36\\tcl\\tk8.6\"\r\n\r\ninclude_files = [\r\n    r\"C:\\Program Files\\Python36\\DLLs\\tcl86t.dll\",\r\n    r\"C:\\Program Files\\Python36\\DLLs\\tk86t.dll\",\r\n]\r\n\r\nbuild_exe_options = {\r\n    \"packages\":[\"os\",\"tkinter\",\"requests\",\"idna\"],\r\n    \"include_files\":include_files\r\n}\r\n\r\nbase = None\r\n\r\nif sys.platform == \"win32\":\r\n    base = \"Win32GUI\"\r\n\r\nsetup(name = \"translate_tool\",\r\n    version = \"0.1\",\r\n    description = \"fanyitools!\",\r\n    options = {\"build_exe\":build_exe_options},\r\n    executables = {Executable(\"windows.py\",base=base,icon='img.ico')}\r\n    )\r\n<\/pre>\n

git\u6e90\u7801\u5730\u5740\uff1ahttps:\/\/github.com\/daily-scripts\/my-python-code\/tree\/master\/FANYI<\/p>\n

\u4e8c\u3001\u6548\u679c\u5c55\u793a<\/strong><\/div>\n

\u8fd0\u884cwindows.py\uff0c\u8f93\u5165\u60f3\u8981\u7ffb\u8bd1\u7684\u5185\u5bb9\uff0c\u70b9\u51fb\u7ffb\u8bd1\u5373\u53ef\u67e5\u770b\u7ffb\u8bd1\u7ed3\u679c
\n\"\"
\n\"\"<\/p>\n

\u53ef\u4ee5\u5229\u7528cx_Freeze\u6253\u5305\u6210windows\u7684mis\u5b89\u88c5\u5c0f\u7a0b\u5e8f\uff0c\u65b9\u4fbf\u4f7f\u7528
\n\"\"
\n\u5207\u6362\u5230\u9879\u76ee\u76ee\u5f55\u4e0b\u6267\u884cpython setup.py bdist_msi
\n\u5f85\u6267\u884c\u5b8c\u6bd5\uff0c\u53ef\u4ee5\u9879\u76ee\u6587\u4ef6\u4e0b\u751f\u6210\u4e24\u4e2a\u6587\u4ef6\u5939dist\u4e2d\u4e3amsi\u5b89\u88c5\u6587\u4ef6\uff0c\u5728\u5176\u4ed6windows\u670d\u52a1\u5668\u5b89\u88c5\u540e\u5c31\u4e3abuild\u4e0b\u7684\u6587\u4ef6\u5185\u5bb9\uff0c\u5728build\u4e0b\u7684exe.win-amd64-3.6\u4e0b\u7684windows.exe \u5c31\u53ef\u6253\u5f00\u5c0f\u5de5\u5177
\n\"\"
\n\u8fdb\u884c\u5b89\u88c5\u6d4b\u8bd5
\n\"\"
\n\u5b89\u88c5\u5b8c\u6210\u540e\u53ef\u4ee5\u8fd0\u884c\u5b89\u88c5\u76ee\u5f55\u4e0b\u7684windows.exe\u6253\u5f00\u5c0f\u5de5\u5177
\n\"\"<\/p>\n","protected":false},"excerpt":{"rendered":"

fanyi.py\u4ee3\u7801\u5982\u4e0b\uff1a #!\/bin\/env python # -*- coding:utf-8 -*- […]<\/p>\n","protected":false},"author":1329,"featured_media":160711,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-160704","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/160704","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1329"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=160704"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/160704\/revisions"}],"predecessor-version":[{"id":160785,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/160704\/revisions\/160785"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/160711"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=160704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=160704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=160704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}