如何使用Google Translate Python API翻译HTML

如何使用Google Translate Python API翻译HTML,python,google-translate,Python,Google Translate,Google Translate Python API有一个format_uu关键字,可以设置为“html”: 我有一篇新闻文章的HTML,它是使用newspaper3k包检索的: HTML是一个二进制字符串,其开头如下: b'<!DOCTYPE html>\r\n<html xmlns="http://www.w3.org/1999/xhtml" lang="ar" dir="rtl" xmlns:fb="http://www.facebook.com/2008/fbml"

Google Translate Python API有一个format_uu关键字,可以设置为“html”:

我有一篇新闻文章的HTML,它是使用newspaper3k包检索的:

HTML是一个二进制字符串,其开头如下:

b'<!DOCTYPE html>\r\n<html xmlns="http://www.w3.org/1999/xhtml" lang="ar" dir="rtl" xmlns:fb="http://www.facebook.com/2008/fbml" xmlns:og="http://opengraphprotocol.org/schema/">\r\n<head>\r\n\t<!-- Meta, title, CSS, favicons, etc. -->\r\n\t<meta charset="UTF-8" />\r\n\t<meta http-equiv="Conten
这将导致以下错误(bytes类型的对象不可JSON序列化)。我做错了什么

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

~\AppData\Local\conda\conda\envs\xview\lib\json\encoder.py in default(self, o)
    178         """
    179         raise TypeError("Object of type '%s' is not JSON serializable" %
--> 180                         o.__class__.__name__)
    181 
    182     def encode(self, o):

TypeError: Object of type 'bytes' is not JSON serializable
答案是(感谢@abarnert And)通过添加。decode(“UTF-8”),将newspaper3k中的二进制字符串解码为UTF-8,这是JSON和Google Translate喜欢使用的,用于移动有效负载:

html_english=translate_client.translate(
      html_arabic.decode("utf-8"), 
      target_language='en', format_='html')

你所说的“仅以实例记录”是什么意思?当我点击左边的链接时,我得到了。另外,“输入文本可以是纯文本或HTML。”那么如果你只是将文章的HTML作为输入文本发送会发生什么呢?谢谢,我没有看到API页面,我只能找到示例。我现在就试试,马上更新。
html_english=translate_client.translate(
      html_arabic.decode("utf-8"), 
      target_language='en', format_='html')