ro  fr  en  es  pt  ar  zh  hi  de  ru
ART 2.0 ART 3.0 ART 4.0 ART 5.0 ART 6.0 Pinterest

مثال على كود Python الذي يترجم موقع الويب إلى لغات أخرى ، مع مكتبة googletrans (الإصدار 2)

June 20, 2021, in Leadership and Attitude, by Neculai Fantanaru

يمكنك عرض الكود كاملاً هنا:https://pastebin.com/i3Nd08TX

تثبيتبايثون. ثم قم بتثبيت المكتبتين التاليتين باستخدام مترجم موجه الأوامر (cmd) في Windows10:

ستقوم Python تلقائيًا بترجمة علامات html التالية باستخدام مكتبة googletrans:

py -m pip install "googletrans"
py -m pip install googletrans==4.0.0rc1
py -m pip install "google_trans_new"

ستقوم شفرة Python أيضًا بترجمة محتويات العلامات التالية (Your Text) تلقائيًا ، ولكن فقط إذا تم تأطير هذه العلامات بواسطةو تعليقات أتش تي أم أل.



 class="den_articol" itemprop="name">Your Text
 class="text_obisnuit">Your Text

class="text_obisnuit2">Your Text

class="text_obisnuit2">Your Text class="text_obisnuit">Your Text class="text_obisnuit">Your Text class="linkMare" href="https://neculaifantanaru.com/ar/">Your Text class="text_obisnuit2>Your Text

text_obisnuit2>Your Text

class="text_obisnuit2>Your Text

الكود: انسخ الكود أدناه وقم بتشغيله في أي برنامج مترجم فوري (أنا استخدم ملفات pyScripter).لا تنس تغيير المسار في سطر "files_from_folder".وهنا لائحة اللغات التي يمكن ترجمتها:لانج.

import os
import re
import textwrap
import html

#-------------------------------------------------------------------------------
# aici pui numlele librarii folosita pentru traducere: ai optiunile google_trans_new sau googletrans
librarie_folosita = "googletrans"
# calea catre folder-ul cu documente de tradus
fisiere_din_folder = r"d:\Downloads\Test"
source_language = 'en'
# in ce limba vreau sa traduc
# {'af': 'afrikaans', 'sq': 'albanian', 'am': 'amharic', 'ar': 'arabic', 'hy': 'armenian', 'az': 'azerbaijani', 'eu': 'basque', 'be': 'belarusian', 'bn': 'bengali', 'bs': 'bosnian', 'bg': 'bulgarian', 'ca': 'catalan', 'ceb': 'cebuano', 'ny': 'chichewa', 'zh-cn': 'chinese (simplified)', 'zh-tw': 'chinese (traditional)', 'co': 'corsican', 'hr': 'croatian', 'cs': 'czech', 'da': 'danish', 'nl': 'dutch', 'en': 'english', 'eo': 'esperanto', 'et': 'estonian', 'tl': 'filipino', 'fi': 'finnish', 'fr': 'french', 'fy': 'frisian', 'gl': 'galician', 'ka': 'georgian', 'de': 'german', 'el': 'greek', 'gu': 'gujarati', 'ht': 'haitian creole', 'ha': 'hausa', 'haw': 'hawaiian', 'iw': 'hebrew', 'hi': 'hindi', 'hmn': 'hmong', 'hu': 'hungarian', 'is': 'icelandic', 'ig': 'igbo', 'id': 'indonesian', 'ga': 'irish', 'it': 'italian', 'ja': 'japanese', 'jw': 'javanese', 'kn': 'kannada', 'kk': 'kazakh', 'km': 'khmer', 'ko': 'korean', 'ku': 'kurdish (kurmanji)', 'ky': 'kyrgyz', 'lo': 'lao', 'la': 'latin', 'lv': 'latvian', 'lt': 'lithuanian', 'lb': 'luxembourgish', 'mk': 'macedonian', 'mg': 'malagasy', 'ms': 'malay', 'ml': 'malayalam', 'mt': 'maltese', 'mi': 'maori', 'mr': 'marathi', 'mn': 'mongolian', 'my': 'myanmar (burmese)', 'ne': 'nepali', 'no': 'norwegian', 'ps': 'pashto', 'fa': 'persian', 'pl': 'polish', 'pt': 'portuguese', 'pa': 'punjabi', 'ro': 'romanian', 'ru': 'russian', 'sm': 'samoan', 'gd': 'scots gaelic', 'sr': 'serbian', 'st': 'sesotho', 'sn': 'shona', 'sd': 'sindhi', 'si': 'sinhala', 'sk': 'slovak', 'sl': 'slovenian', 'so': 'somali', 'es': 'spanish', 'su': 'sundanese', 'sw': 'swahili', 'sv': 'swedish', 'tg': 'tajik', 'ta': 'tamil', 'te': 'telugu', 'th': 'thai', 'tr': 'turkish', 'uk': 'ukrainian', 'ur': 'urdu', 'uz': 'uzbek', 'vi': 'vietnamese', 'cy': 'welsh', 'xh': 'xhosa', 'yi': 'yiddish', 'yo': 'yoruba', 'zu': 'zulu', 'fil': 'Filipino', 'he': 'Hebrew'}
destination_language = 'ru'
delimitatori_text_exterior_articol = [['How To Make Python Code For Google Translate and Beautifulsoup Library (الإصدار 2)  | Neculai Fantanaru (ar) ']]
delimitatori_text_interior_articol = [['

'

'
], ['

'

'
], ['''], ['
  • '
  • '
    ], ['

    '

    '
    ], ['

    '

    '
    ], [', '']] extensie_fisier = ".html" #------------------------------------------------------------------------------- if (librarie_folosita == "google_trans_new"): from google_trans_new import google_translator translator = google_translator() elif (librarie_folosita == "googletrans"): from googletrans import Translator translator = Translator() elif (librarie_folosita == "translators"): import translators as ts lista_cale_fisiere = [] VAR, REPL = re.compile(r'(<.*?>)'), re.compile(r'_____(\d+)_____') varlist = [] def replace(matchobj): varlist.append(matchobj.group()) return "_____%d_____" %(len(varlist)-1) def restore(matchobj): try: return varlist[int(matchobj.group(1))] except: a=9 def traducere_text(text): result = "" for txt in (textwrap.wrap(text, 4500, break_long_words=False)): # impart in maxim 4500 de caractere uitandu-ma dupa spatii txt = html.unescape(txt) # Convert all named and numeric character references (e.g. >, >, >) in the string s to the corresponding Unicode characters. if (librarie_folosita == "google_trans_new"): while (re.search(r'(<.*?>)', txt)) : txt = VAR.sub(replace, txt) translation = translator.translate(txt, lang_tgt=destination_language) while (re.search(r'_____[0-9 ]+ _____', translation)): rep = re.search(r'_____[0-9 ]+ _____', translation).group(0) translation = translation.replace(rep, rep.replace(r' ', r'')) while (re.search(r'_____ [0-9 ]+_____', translation)): rep = re.search(r'_____ [0-9 ]+_____', translation).group(0) translation = translation.replace(rep, rep.replace(r' ', r'')) translation = REPL.sub(restore, translation) result = result + translation elif (librarie_folosita == "googletrans"): while (re.search(r'(<.*?>)', txt)) : txt = VAR.sub(replace, txt) translation = translator.translate(txt, dest=destination_language).text while (re.search(r'_____[0-9 ]+ _____', translation)): rep = re.search(r'_____[0-9 ]+ _____', translation).group(0) translation = translation.replace(rep, rep.replace(r' ', r'')) while (re.search(r'_____ [0-9 ]+_____', translation)): rep = re.search(r'_____ [0-9 ]+_____', translation).group(0) translation = translation.replace(rep, rep.replace(r' ', r'')) translation = REPL.sub(restore, translation) result = result + translation elif (librarie_folosita == "translators"): while (re.search(r'(<.*?>)', txt)) : txt = VAR.sub(replace, txt) translation = translator.translate(txt, dest=destination_language).text while (re.search(r'_____[0-9 ]+ _____', translation)): rep = re.search(r'_____[0-9 ]+ _____', translation).group(0) translation = translation.replace(rep, rep.replace(r' ', r'')) while (re.search(r'_____ [0-9 ]+_____', translation)): rep = re.search(r'_____ [0-9 ]+_____', translation).group(0) translation = translation.replace(rep, rep.replace(r' ', r'')) translation = REPL.sub(restore, translation) result = result + translation return result def selectare_traducere_continut(cont, delimitatori): for delimitator in delimitatori: start_delim = delimitator[0] # ' stop_delim = delimitator[1] # '' start_position = 0 stop_position = len(cont)-1 while cont[start_position:stop_position].find(start_delim)>0: temp_st = cont[start_position:stop_position].find(start_delim) + len(start_delim) + start_position temp = temp_st + cont[temp_st:stop_position].find('>') if (cont[temp-1] == '/'): start_position = temp else: start_position = temp+1 st = cont[start_position:stop_position].find(stop_delim) + start_position extracted_text = cont[start_position:st] translated_text = traducere_text(extracted_text) cont = cont[:start_position] + translated_text + cont[st:] start_position = start_position + len(translated_text) return cont def selectare_text(): for file in os.listdir(fisiere_din_folder): if file.endswith(extensie_fisier): lista_cale_fisiere.append(os.path.join(fisiere_din_folder, file)) for fisier in lista_cale_fisiere: f = open(fisier, 'r') if f.mode == 'r': contents = f.read() #contents = html.unescape(contents) # Convert all named and numeric character references (e.g. >, >, >) in the string s to the corresponding Unicode characters. print ("Acum lucrez la fisierul :", fisier) continut = [] if (contents.find('>0): # am gasit '' in pagina pozitia1 = contents.find('+ len('= pozitia1 + contents[pozitia1+1:].find(">") - 1 selectie = contents[pozitia1:pozitia2] trad = traducere_text(selectie) contents = contents[:pozitia1] + trad + contents[pozitia2:] if (contents.find('')>0): # am gasit '' in pagina poz1 = contents.find('') + len('') poz2 = contents.find('') + len('') inceput = contents[:poz1] articol = contents[poz1:poz2] final = contents[poz2:] continut.append(inceput) continut.append(articol) continut.append(final) else: continut.append(contents) continut_tradus = '' if (len(continut) == 3): continut_tradus = continut_tradus + selectare_traducere_continut(continut[0], delimitatori_text_exterior_articol) # inceput continut_tradus = continut_tradus + selectare_traducere_continut(continut[1], delimitatori_text_interior_articol) # articol continut_tradus = continut_tradus + selectare_traducere_continut(continut[2], delimitatori_text_exterior_articol) # exterior elif (len(continut) == 1): continut_tradus = continut_tradus + selectare_traducere_continut(continut[0], delimitatori_text_exterior_articol) else: print ("S-a produs o eroare cand am vrut sa citesc articolul sau exteriorul lui !") print("Am citit un fisier si incep traducerea!\n") with open(fisier[:len(fisier)-len(extensie_fisier)]+"_"+destination_language+extensie_fisier, 'w', encoding="utf-8") as f: f.write(continut_tradus) print("Am terminat traducerea !") selectare_text()

    That's all folks.

    If you like my code, then make me a favor: translate your website into Romanian, "ro".

    أيضا ، هناك ملفالنسخة 1من هذا الرمز.

     


    أحدث المقالات التي تم الوصول إليها من قبل القراء:

    1. عين لرؤية وعقل لفهم
    2. استدر نحوي بعين مليئة بنظراتك الخاصة
    3. لقطة من السحر في عالم الله
    4. إيقاع قلبي
    ( )

    Donate via Paypal

    Alternate Text

    RECURRENT DONATION

    Donate monthly to support
    the NeculaiFantanaru.com project

    SINGLE DONATION

    Donate the desired amount to support
    the NeculaiFantanaru.com project

    Donate by Bank Transfer

    Account Ron: RO34INGB0000999900448439

    Open account at ING Bank

    Join The Neculai Fantanaru Community



    * Note: If you want to read all my articles in real time, please check the romanian version !

    decoration
    About | Site Map | Partners | Feedback | Terms & Conditions | Privacy | RSS Feeds
    © Neculai Fântânaru - All rights reserved