Chirurgia cranio-cerebrală minim invazivă
Tehnicile minim invazive impun utilizarea unei tehnologii ultramoderne. Endoscoapele operatorii de diverse tipuri, microscopul operator dedicat, neuronavigația, neuroelectrofiziologia, tehnicile avansate de anestezie, chirurgia cu pacientul treaz reprezintă armamentarium fără de care neurochirurgia prin "gaura cheii" nu ar fi posibilă. Folosind tehnicile de mai sus, tratăm un spectru larg de patologii cranio-cerebrale. www.neurohope.ro |
Python: De ce nu pot sa traduc un singur html tag din toate celelalte
Last Updated: Oct 15 2022 17:26, Started by
me_suzy
, Oct 11 2022 08:53
·
0
#1
Posted 11 October 2022 - 08:53
Am acest cod python care funcționează foarte bine.
Asadar, pot să traduc toate tagurile HTML, cu excepția unuia: `<meta name="description" content="...">` ma poate ajuta cineva cu o solutie? Nu știu de ce nu funcționează acest tag sa-l traduc. 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> |
#2
Posted 11 October 2022 - 09:53
Vezi ca nu ai tagul <body> - il ai doar pe cel de inchidere </body>
|
#3
Posted 11 October 2022 - 09:56
dev2000, on 11 octombrie 2022 - 09:53, said:
Vezi ca nu ai tagul <body> - il ai doar pe cel de inchidere </body> Nu are nicio legatura cu <body>. De fapt, poti elimina tot din codul html, poti lasa doar tagurile alea. Traduce la fel. Problema este la tagul 'meta-description' si la codul Python. Nu are legatura cu html. Am pus html ca sa se vada bine. Dar poti lasa doar tagurile in fisierul html. Edited by me_suzy, 11 October 2022 - 09:59. |
#4
Posted 11 October 2022 - 11:06
Quote
For Python 2.6 and later and Python 3.x: except Exception as e: print(e) For Python 2.5 and earlier, use: except Exception,e: print str(e) Vezi de ce iti trece prin pass |
#5
Posted 11 October 2022 - 11:12
#6
Posted 11 October 2022 - 12:22
for meta in soup.findAll('meta', {'name':'description'}): try: meta['content'] = recursively_translate(meta['content']) except: pass In lipsa de alte detalii banuiesc ca esueaza try-ul si pleaca in except, doar ca tu in cod nu tratezi eroarea ci il trimiti direct mai departe. |
#7
Posted 11 October 2022 - 12:26
am scos si try, dupa cum se vede mai jos,
for meta in soup.findAll('meta', {'name':'description'}): meta['content'] = recursively_translate(meta['content']) am obtinut eroarea: [ https://snipboard.io/69uWyz.jpg - Pentru incarcare in pagina (embed) Click aici ] |
#8
Posted 11 October 2022 - 17:24
Si de unde ai copiat codul ?
Edited by MarianG, 11 October 2022 - 17:24. |
#9
Posted 11 October 2022 - 17:25
de pe net.
Edited by MarianG, 11 October 2022 - 17:30.
|
#10
Posted 11 October 2022 - 17:30
n-as fi ghicit ...
dar de unde? Edited by MarianG, 11 October 2022 - 17:31. |
|
#11
Posted 11 October 2022 - 17:31
Oare rezolvarea problemei o poti ghici?
Edited by MarianG, 11 October 2022 - 17:31. |
#12
Posted 11 October 2022 - 17:33
oare daca nu ati prezenta cod care sa fie luat aleatoriu "de pe net"
si ati veni cu cod scris si gandit de voi nu ar fi mai usor sa va ajutam in procesul de invatare? Asa "de pe net", te rog frumos sa mergi sa ii intrebi pe aia de ti-au oferit codul, care e rezolvarea. Edited by MarianG, 11 October 2022 - 17:34. |
#13
Posted 15 October 2022 - 17:26
Acest cod mi-a fost trimis prin mesageria interna (de catre initiator).
for meta in soup.findAll('meta', {'name':'description'}): ----try: --------meta['content'] = translator.translate(meta['content'], dest=destination_language).text ----except: --------passSe sustine ca este rezolvarea problemei. Edited by MarianG, 15 October 2022 - 17:26. |
Anunturi
▶ 0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users