Удалите все теги <u>и <a>из всех тегов <div>класса с помощью BeautifulSoup или re - PullRequest
1 голос
/ 09 мая 2020

Я пытаюсь удалить теги <u> и <a> из всех тегов DIV, которые имеют класс «sf-item» из источника HTML, потому что они нарушают текст при извлечении из веб-адреса.

(для этой демонстрации я назначил образец строки html методу BeautifulSoup, но в идеале это был бы веб-URL в качестве источника)

До сих пор я пробовал использовать re со строкой ниже - но я не уверен, как указать условие в re, чтобы - удалить только подстроку между всеми <u /u> только в тегах DIV класса sf-item

data = re.sub('<u.*?u>', '', data)

Также попытался удалить все Теги <u> и <a> из всего источника с использованием строки ниже, но почему-то это не работает. Не знаю, как указать все теги <u> и <a> только в тегах DIV с классом sf-item.

for tag in soup.find_all('u'):
    tag.replaceWith('')

Благодарю, если вы могли бы помочь мне в этом.

Ниже приведен пример кода Python, который работает -

from re import sub
from bs4 import BeautifulSoup
import re

data = """
<div class="sf-item"> The rabbit got to the halfway point at   
<u><a href="https://DummyLocationURL/"> here </a></u> However, it couldn't see the turtle. 
</div>
<div class="sf">
<div class="sf-item sf-icon">
<span class="supporticon is"></span>
</div>
<div class="sf-item"> He was hot and tired and decided to stop and take a short nap. 
</div>
<div class="sf-item"> Even if the turtle passed him at 
<u><a href="https://DummyLocationURL/">Link</a></u>. he would be able to race to the finish line ahead of 
<u><a href="https://DummyLocationURL/">place</a></u>, he just kept going.
</div>
"""

# data = re.sub('<u.*?u>', '', data)  ## This works for this particular string but I cannot use on a web url
# It would solve if I can somehow specify to remove <u> and <a> only within DIV of class sf-item

soup = BeautifulSoup(data, "html.parser")

for tag in soup.find_all('u'):
    tag.replaceWith('')

fResult = []
rMessage=soup.findAll("div",{'class':"sf-item"})

for result in rMessage:
    fResult.append(sub("&ldquo;|.&rdquo;","","".join(result.contents[0:1]).strip()))

fResult = list(filter(None, fResult))
print(fResult)

Вывод, который я получаю из приведенного выше кода:

['The rabbit got to the halfway point at', 'He was hot and tired and decided to stop and take a short nap.', 'Even if the turtle passed him at']

Но мне нужен вывод, как показано ниже -

['The rabbit got to the halfway point at here However, it couldnt see the turtle.', 'He was hot and tired and decided to stop and take a short nap.', 'Even if the turtle passed him at Link. he would be able to race to the finish line ahead of place, he just kept going.']

1 Ответ

2 голосов
/ 09 мая 2020

BeautifulSoup имеет встроенный метод для получения видимого текста из тега (т. Е. Текста, который будет отображаться при отображении в браузере). Запустив следующий код, я получаю ожидаемый результат:

from re import sub
from bs4 import BeautifulSoup
import re

data = """
<div class="sf-item"> The rabbit got to the halfway point at   
<u><a href="https://DummyLocationURL/"> here </a></u> However, it couldn't see the turtle. 
</div>
<div class="sf">
<div class="sf-item sf-icon">
<span class="supporticon is"></span>
</div>
<div class="sf-item"> He was hot and tired and decided to stop and take a short nap. 
</div>
<div class="sf-item"> Even if the turtle passed him at 
<u><a href="https://DummyLocationURL/">Link</a></u>. he would be able to race to the finish line ahead of 
<u><a href="https://DummyLocationURL/">place</a></u>, he just kept going.
</div>
"""

soup = BeautifulSoup(data, "html.parser")

rMessage=soup.findAll("div",{'class':"sf-item"})

fResult = []

for result in rMessage:
    fResult.append(result.text.replace('\n', ''))

Это даст вам правильный результат, но с некоторыми дополнительными пробелами. Если вы хотите сократить их все до отдельных пробелов, вы можете запустить fResult следующим образом:

fResult = [re.sub(' +', ' ', result) for result in fResult]

...