简体   繁体   中英

How to Translate a webpage with googletrans API in python?

from googletrans import Translator
import requests

translator = Translator()

url = 'http://www.compactrb.ro/'

page = requests.get(url)

translated = translator.translate(page.text)

print(translated)

My code looks something like this, I want to get the webpage using the requests library and I want to translate this webpage into English. This webpage is in Romanian, I want to perform scraping on this website but first I want to convert the webpage into English. This code does not work it gives me this error : TypeError: the JSON object must be str, bytes or bytearray, not NoneType . If there is another way to do this please let me know.

The default googletrans package has not been updated in years and currently has a bug in it.

You can fix this by installing the alpha release: pip install googletrans==3.1.0a0

I've tested and it works with the alpha version.

You can follow the discussion / updates here: https://github.com/ssut/py-googletrans/issues/234#issuecomment-742460612

from bs4 import BeautifulSoup
from bs4.formatter import HTMLFormatter
from googletrans import Translator
import requests

translator = Translator()

class UnsortedAttributes(HTMLFormatter):
    def attributes(self, tag):
        for k, v in tag.attrs.items():
            yield k, v

files_from_folder = r"e:\Carte\Translate"

use_translate_folder = False

destination_language = 'fr'

extension_file = ".html"

import os

directory = os.fsencode(files_from_folder)

See the complete code HERE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM