【问题标题】:I want to change html content with my own我想用自己的方式更改 html 内容
【发布时间】:2021-08-19 01:37:28
【问题描述】:

我正在使用 lxml 和 beautifulsoup 库,实际上我的目标是从整个 html 代码中翻译特定标签的文本,我使用翻译库成功翻译它,但我想要的是,我想替换带有翻译文本的特定标签的文本。

这里是html代码:

<p class="text_obisnuit2">What is a performer?</p>
<p class="text_obisnuit2">Leadership: Performer</p>
<p class="text_obisnuit2">Question:</p>

所以上面的 html 有英文文本。我想要的输出类型应该是罗马尼亚语:

<p class = "text_obisnuit2"> Ce este un interpret? </p>
<p class = "text_obisnuit2"> Conducere: interpret </p>
<p class = "text_obisnuit2"> Întrebare: </p>

我想你们已经明白了。

所以我想为特定的 xpath 设置一个循环,所有翻译的文本都应该一个接一个地插入其中。

这是我的代码:

from bs4 import BeautifulSoup, NavigableString, Tag
import requests
import time
import pandas as pd
import translators as ts
import json
import numpy as np
import regex
import selenium
from lxml import html
import time
import lxml.html

#r=requests.get(input('Enter the URL of your HTML page:\n'))
r=requests.get('https://neculaifantanaru.com/en/definition-what-is-a-performer.html')
soup=BeautifulSoup(r.text, 'html.parser')
page=r.content
element = html.fromstring(page)

    
for item in element.xpath('//div[@align = "justify"]/p[@class = "text_obisnuit2"]'):  
    text=item.text.content()    
    output=ts.google(text, from_language='en', to_language='ro') 

    for z in soup.find_all('p', attrs={'class':'text_obisnuit2'}):
        var1=z.string

        var1.replace_with(var1.replace(var1, output))
        print(soup)

我得到的输出:

<p class="text_obisnuit2">Ce este un interpret? </p>
<p class="text_obisnuit2">Ce este un interpret? </p>
<p class="text_obisnuit2">Ce este un interpret? </p>

AttributeError: 'NoneType' object has no attribute 'replace_with'

我想要的输出:

<p class = "text_obisnuit2"> Ce este un interpret? </p>
<p class = "text_obisnuit2"> Conducere: interpret </p>
<p class = "text_obisnuit2"> Întrebare: </p>

注意:

应该有一个循环在所有这些标签中插入翻译后的文本,我的意思是所有标签都应该在使用循环翻译后得到自己的文本。

我无法解释更多,请任何人指导我。

【问题讨论】:

    标签: python xpath beautifulsoup python-requests lxml


    【解决方案1】:

    要将标签替换为翻译后的文本,请使用replace_with(),如下所示:

    import translators as ts
    from bs4 import BeautifulSoup
    
    html = """
    <p class="text_obisnuit2">What is a performer?</p>
    <p class="text_obisnuit2">Leadership: Performer</p>
    <p class="text_obisnuit2">Question:</p>
    """
    
    soup = BeautifulSoup(html, "html.parser")
    
    for tag in soup.find_all(class_="text_obisnuit2"):
        translated_text = ts.google(tag.text, from_language="en", to_language="ro")
        tag.replace_with(translated_text)
    
    print(soup.prettify())
    

    输出:

    Ce este un interpret?
    Leadership: interpret
    Întrebare:
    

    注意:translators 库没有很好地翻译“Leadership: Performer”文本的输出。

    【讨论】:

    • 只是一个评论来帮助用户询问:var1.replace_with(var1.replace(var1, output)) 我认为是递归调用的。 replace_with 的参数应该是一个字符串而不是另一个变量。见here
    • 我将您的代码编辑为:tag.string.replace_with(tag.string.replace(tag.string,translated_text)),因此会返回整个 html 代码,而不仅仅是文本。顺便感谢您的帮助。
    • 按照你的方法,翻译库不能翻译超过5000字的标签文本,所以我用了我找到的其他方式,看看我的答案。
    【解决方案2】:

    后来发现这个方法,很好,用这个方法我连5000字以上的文章都能翻译。

    articles=[]
    for item in element.xpath('//div[@align = "justify"]/p[@class = "text_obisnuit"]'):  
    
        #texts=item.text_content()
        #texts=texts.split('"', 50)
        articles.append(item.text_content())
    
    translated_articles=[]
    for text in articles:
        output=ts.google(text, from_language='en', to_language='ro')
        translated_articles.append(output)
        for i,z in zip(translated_articles,soup.find_all('p', attrs={'class':'text_obisnuit'})):
            var=z.string
            var.replace_with(var.replace(var, i))
    
    
    #print(soup)
    

    【讨论】:

      猜你喜欢
      • 2021-08-18
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多