Reputation: 40
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.
Upvotes: 0
Views: 2196
Reputation: 1053
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
Upvotes: 0
Reputation: 1928
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
Upvotes: 3