Python鐖櫕瀹炶返 鈥斺€?3.鍒╃敤鐖櫕鎻愬彇杩斿洖鍊硷紝妯℃嫙鏈夐亾璇嶅吀鎺ュ彛
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python鐖櫕瀹炶返 鈥斺€?3.鍒╃敤鐖櫕鎻愬彇杩斿洖鍊硷紝妯℃嫙鏈夐亾璇嶅吀鎺ュ彛相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e5%9b%be%e7%89%87' title='鍥剧墖'>鍥剧墖 澶栭儴 client tgt you ret smart real 鐖櫕
鏈夐亾璇嶅吀鐨剋eb鎺ュ彛锛屽疄闄呬笂鍙互鐢ㄧ埇铏ā鎷燂紝杈撳叆key锛屾嫾鎺ヤ负鏈夐亾璇嶅吀鎺ュ彛鐨刦ormdata锛岀埇鍙栬繑鍥炲€硷紝瀹為檯涓篈jax鍔ㄦ€佺敓鎴愮殑translation锛岃繖鏍峰閮ㄦ潵鐪嬪疄鐜颁簡缈昏瘧鎺ュ彛鐨勬ā鎷燂紝鐩稿綋浜庣埇铏ā鎷熸祻瑙堝櫒璋冪敤浜嗘湁閬撹瘝鍏竪eb鎺ュ彛锛屽叾瀹炶鐪熺殑璇濇潵璇达紝鐩存帴璋冪敤鏈夐亾web鎺ュ彛锛屼紶json鍙傛暟灏卞彲浠ヤ簡锛屼笉鐢ㄨ繖涔堣垂浜嬶紝浣嗙埇铏ā鎷熶簡浜虹櫥闄唚eb锛岃緭鍏ュ叧閿瘝锛岃幏寰楃炕璇戠粨鏋滅殑杩囩▼銆?/p>
娴忚鍣ㄨ緭鍏ユ搷浣滐紝瑙f瀽鏈夐亾璇嶅吀缈昏瘧鐨剋eb鎺ュ彛url鍜屾牸寮?/p>
#鐖櫕妯℃嫙璋冪敤鏈夐亾璇嶅吀web鎺ュ彛璋冪敤 from urllib import request from urllib import parse import re class YoudaoTranslator: def __init__(self, key): self.key = key def __getData(self): # 鏋勯€?鏈夐亾璇嶅吀web鎺ュ彛鎵€闇€鐨凢orm data formdata = { "i": self.key, "from": "AUTO", "to": "AUTO" , "smartresult": "dict", "client": "fanyideskweb", "salt": "15763837022114", "sign": "2b12fd214e066f53bc3455a126d7a509", "ts": "1576383702211", "bv": "5575008ba9785f184b106838a72d6536", "doctype": "json", "version": "2.1", "keyfrom": "fanyi.web", "action": "FY_BY_REALTlME" } data = parse.urlencode(formdata).encode(encoding="utf-8") return data def __getPage(self): #鑾峰緱妯℃嫙娴忚鍣ㄨ姹傦紝鑾峰緱Ajax杩斿洖鍊?/span> header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36"} url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule" req = request.Request(url, data=YoudaoTranslator.__getData(self), headers=header) res = request.urlopen(req).read().decode() return res def __Pat(self): #瑙f瀽ajax杩斿洖json瀛楃涓诧紝姝e垯鍖归厤鑾峰彇缈昏瘧鍊?/span> pat = r鈥?/span>"tgt":"(.*?)"}]]鈥?/span> result = re.findall(pat, YoudaoTranslator.__getPage(self)) print(result[0]) return result def Translator(self): YoudaoTranslator.__Pat(self) if __name__ == 鈥?/span>__main__鈥?/span>: i = YoudaoTranslator("浜烘牸蹇冪悊瀛?/span>") i.Translator()
鐒跺悗鏄繍琛岀粨鏋?/p>
以上是关于Python鐖櫕瀹炶返 鈥斺€?3.鍒╃敤鐖櫕鎻愬彇杩斿洖鍊硷紝妯℃嫙鏈夐亾璇嶅吀鎺ュ彛的主要内容,如果未能解决你的问题,请参考以下文章