众希网站建设,企业如何申请网站,wordpress 网站运行时间,网站付费模板欸#xff0c;最近想做一些nlp的项目#xff0c;做完了中文的想做做英文的#xff0c;但是呢#xff0c;国内爬虫爬取的肯定都是中文 #xff0c;爬取外网的技术我没有尝试过#xff0c;没有把握。所以我决定启用翻译#xff0c;在这期间chatGPT给了我非常多的方法… 欸最近想做一些nlp的项目做完了中文的想做做英文的但是呢国内爬虫爬取的肯定都是中文 爬取外网的技术我没有尝试过没有把握。所以我决定启用翻译在这期间chatGPT给了我非常多的方法但是都因为各种各样的原因一一无效。ps:大骗子GPT
sample1: from youdaoapi import YoudaoApi def translate_to_english(chinese_text): youdao YoudaoApi() english_translation youdao.translate(chinese_text, to_langen) return english_translation chinese_string 你好世界 english_translation translate_to_english(chinese_string) print(English Translation:, english_translation) NOTE无用的youdaoapi包
sample2: from translate import Translator def translate_to_english(chinese_text): translator Translator(to_langen) english_translation translator.translate(chinese_text) return english_translation chinese_string 你好世界 english_translation translate_to_english(chinese_string) print(English Translation:, english_translation) NOTE无用的translate包。
sample3: from googletrans import Translator def translate_to_english(chinese_text): translator Translator() translated translator.translate(chinese_text, srczh-cn, desten) return translated.text chinese_string 你好世界 english_translation translate_to_english(chinese_string) print(English Translation:, english_translation) NOTE:无用的谷歌包。
百度api,yyds!非常nice
Step1、申请 百度文本翻译api申请整体流程页面https://console.bce.baidu.com/ai/?_1652768945367fromai1#/ai/machinetranslation/overview/index Step2、第二步完成直接看我的代码只需要API Key和Secret Key便可以使用。 # -*- coding: utf-8 -*-# This code shows an example of text translation from English to Simplified-Chinese.
# This code runs on Python 2.7.x and Python 3.x.
# You may install requests to run this code: pip install requests
# Please refer to https://api.fanyi.baidu.com/doc/21 for complete api documentimport requests
import random
import jsondef get_access_token():使用 AKSK 生成鉴权签名Access Tokenclient_idAPI Keyclient_secretSecret Key:return: access_token或是None(如果错误)url https://aip.baidubce.com/oauth/2.0/tokenparams {grant_type: client_credentials, client_id: 5UHGfQaGLKlINhXRv1lA0tl3, client_secret: evGZuz1r14MRElOt638D8GMdheQ9gKZj}return str(requests.post(url, paramsparams).json().get(access_token))def baidu_translate(q):token get_access_token()url https://aip.baidubce.com/rpc/2.0/mt/texttrans/v1?access_token token# For list of language codes, please refer to https://ai.baidu.com/ai-doc/MT/4kqryjku9#语种列表from_lang zh # example: ento_lang en # example: zhterm_ids # 术语库id多个逗号隔开# Build requestheaders {Content-Type: application/json}payload {q: q, from: from_lang, to: to_lang, termIds : term_ids}# Send requestr requests.post(url, paramspayload, headersheaders)result r.json()# Show response# print(json.dumps(result, indent4, ensure_asciiFalse))return result[result][trans_result][0][dst]效果截图 耶耶耶尽情享用吧爬取数据的同时加一个这个函数转成英文再储存为csv等完美解决这个小小的难题。值得记录一下勒----