Как удалить все HTML-теги и объединить текст - PullRequest
0 голосов
/ 07 февраля 2019

Я использую BeautifulSoup.

review =page_soup.findAll("div",{"class":"content"})

ниже приведены результаты моего обзора

 review    = [<div class="content">
    <div class="text show-more__control">AAAAA.</div>
    <div class="actions text-muted">
                        3 out of 8 found this helpful.
                            <span>
                                Was this review helpful? <a href="/registration/signin?ref_=urv"> Sign in</a> to vote.
                            </span>
    <br/>
    <a href="/review/rw1429145/?ref_=tt_urv">Permalink</a>
    </div>
    </div>, <div class="content">
    <div class="text show-more__control">BBBBB.</div>
    <div class="actions text-muted">
                        1 out of 2 found this helpful.
                            <span>
                                Was this review helpful? <a href="/registration/signin?ref_=urv"> Sign in</a> to vote.
                            </span>
    <br/>
    <a href="/review/rw2895175/?ref_=tt_urv">Permalink</a>
    </div>
    </div>]

Я хочу изменить его на необработанный текст, как этот.

AAAAA.BBBBB.

1 Ответ

0 голосов
/ 07 февраля 2019

Вы можете получить text на div с классом show-more__control.

divs=page_soup.find_all('div',class_="show-more__control")
texts=[x.text for x in divs]
print(''.join(texts))

Если show_more__control присутствует в другом месте, вы можете использовать

contents=page_soup.find_all('div',class_="content")
texts=[x.find('div',class_='show-more__control').text for x in contents]
print(''.join(texts))

Изменить : отредактированный ответ для отражения изменений в вашем вопросе

HTML-код исходного вопроса с ответом

html="""
<div class="content">
<div class="text show-more__control">AAAAA.<br/><br/>Ted's Evaluation -- 1 of 3: You can find something better to do with this part of your life.</div>
<div class="actions text-muted">
                    3 out of 8 found this helpful.
                        <span>
                            Was this review helpful? <a href="/registration/signin?ref_=urv"> Sign in</a> to vote.
                        </span>
<br/>
<a href="/review/rw1429145/?ref_=tt_urv">Permalink</a>
</div>
</div>, <div class="content">
<div class="text show-more__control">BBBBB.</div>
<div class="actions text-muted">
                    1 out of 2 found this helpful.
                        <span>
                            Was this review helpful? <a href="/registration/signin?ref_=urv"> Sign in</a> to vote.
                        </span>
<br/>
<a href="/review/rw2895175/?ref_=tt_urv">Permalink</a>
</div>
</div>
"""
from bs4 import BeautifulSoup
soup=BeautifulSoup(html,"html.parser")
divs=soup.find_all('div',class_="show-more__control")
texts=[x.contents[0] for x in divs]
print(''.join(texts))

Вывод:

AAAAA.BBBBB.

Просто с помощью textатрибут в этом случае дал бы вывод

AAAAA.Ted's Evaluation -- 1 of 3: You can find something better to do with this part of your life.BBBBB.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...