1

When I try to get the Arabic translation from Google translate it show me this:

'charmap' codec can't encode characters in position 0-3: character maps to

The code is:

driver.get("https://translate.google.com/?hl=ar&sl=ar&tl=en&text=%D9%85%D8%B1%D8%AD%D8%A8%D8%A7&op=translate")
translated_text = driver.find_elements(By.CSS_SELECTOR, 'div[class="A3dMNc"]')
sleep(3)
for i in translated_text:
    p=i.get_attribute("data-initial-text")
    print(p.encode('cp850').decode('cp850'))
halfer
  • 19,824
  • 17
  • 99
  • 186
SLOWDEATH
  • 31
  • 5
  • `'div[class="A3dMNc"]'` matches only 1 element on the page. Why do you need a loop there? – Prophet Sep 25 '22 at 11:08
  • 1
    the question needs sufficient code for a minimal reproducible example: https://stackoverflow.com/help/minimal-reproducible-example (presumably you used selenium or its modules ??) – D.L Sep 25 '22 at 11:09

1 Answers1

0

You don't need the .encode('cp850').decode('cp850') part. Just put the below on top of your code (source):

import sys
sys.stdin.reconfigure(encoding='utf-8')
sys.stdout.reconfigure(encoding='utf-8')
Duc Nguyen
  • 301
  • 2
  • 8