Python Forum
Why is the 'meta description' html tag not translated?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is the 'meta description' html tag not translated?
#1
I have this python code which works very well.

But I can translate all my HTML tags except this one: <meta name="description" content="...">

can anyone help me with a solution? I don't know why it doesn't work.


    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"c:\carte\1"
    
    use_translate_folder = True
    
    destination_language = 'fr'
    
    extension_file = ".html"
    
    import os
    
    directory = os.fsencode(files_from_folder)
    
    def recursively_translate(node):
        for x in range(len(node.contents)):
            if isinstance(node.contents[x], str):
                if node.contents[x].strip() != '':
                    try:
                        node.contents[x].replaceWith(translator.translate(node.contents[x], dest=destination_language).text)
                    except:
                        pass
            elif node.contents[x] != None:
                recursively_translate(node.contents[x])
    
    amount = 1
    for file in os.listdir(directory):
        filename = os.fsdecode(file)
        print(filename)
        if filename == 'y_key_e479323ce281e459.html' or filename == 'directory.html':
            continue
        if filename.endswith(extension_file):
            with open(os.path.join(files_from_folder, filename), encoding='utf-8') as html:
                soup = BeautifulSoup('<pre>' + html.read() + '</pre>', 'html.parser')
                for title in soup.findAll('title'):
                    recursively_translate(title)
    
                for meta in soup.findAll('meta', {'name':'description'}):
                    try:
                        meta['content'] = recursively_translate(meta['content'])
                    except:
                        pass
    
    
                for p in soup.findAll('p', class_='text_obisnuit2'):
                        recursively_translate(p)
    
    
    
            print(f'{filename} translated ({amount})')
            amount += 1
            soup = soup.encode(formatter=UnsortedAttributes()).decode('utf-8')
            new_filename = f'{filename.split(".")[0]}_{destination_language}.html'
            if use_translate_folder:
                try:
                    with open(os.path.join(files_from_folder+r'\translated', new_filename), 'w', encoding='utf-8') as new_html:
                        new_html.write(soup[5:-6])
                except:
                    os.mkdir(files_from_folder+r'\translated')
                    with open(os.path.join(files_from_folder+r'\translated', new_filename), 'w', encoding='utf-8') as new_html:
                        new_html.write(soup[5:-6])
            else:
                with open(os.path.join(files_from_folder, new_filename), 'w', encoding='utf-8') as html:
                    html.write(soup[5:-6])
test.html

    <html>
    <head>
    
        <title>It really helps me do great things for her</title>
    	
    	<meta name="description" content="What I LOVE to do and what I don't love">
    
        <p class="text_obisnuit2"><em>Buckingham has a new book called Love</em></p>
    
    </body>
    </html>
Reply
#2
change as this that part of code:


            for meta in soup.findAll('meta', {'name':'description'}):
                try:
                    meta['content'] = translator.translate(meta['content'], dest=destination_language).text
                except:
                    pass
Reply
#3
what is the goal?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get file description Raysz 2 509 Nov-25-2023, 03:46 PM
Last Post: Raysz
  Tkinterweb (Browser Module) Appending/Adding Additional HTML to a HTML Table Row AaronCatolico1 0 923 Dec-25-2022, 06:28 PM
Last Post: AaronCatolico1
  Formatted string not translated by gettext YvanM 10 1,994 Sep-02-2022, 08:46 PM
Last Post: YvanM
  What should i do, for this code to work -> description hamad 2 1,457 Nov-18-2021, 01:22 PM
Last Post: ghoul
  reading html and edit chekcbox to html jacklee26 5 3,071 Jul-01-2021, 10:31 AM
Last Post: snippsat
  Classes in general description Python_User 2 1,912 Sep-01-2020, 05:30 PM
Last Post: Python_User
  HTML to Python to Windows .bat and back to HTML perfectservice33 0 1,943 Aug-22-2019, 06:31 AM
Last Post: perfectservice33
  Python Newbie Environmental Meta Issue wallacebmann 2 2,397 Apr-26-2019, 02:08 PM
Last Post: Larz60+
  Function to skip meta data in a .csv file using Python ajgardev 1 2,509 Jul-22-2018, 12:53 PM
Last Post: Larz60+
  Google Developer console ERROR: JAR_SIG_NO_MANIFEST: Missing META-INF/MANIFEST.MF kivyuser 5 9,113 Jan-14-2018, 05:35 PM
Last Post: kivyuser

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020