Замените <br>пробелом в выводе BeautifulSoap - PullRequest
1 голос
/ 09 апреля 2019

Я удаляю несколько ссылок с BeautifulSoap, но, похоже, он полностью игнорирует теги <br>.

Вот соответствующая часть исходного кода URL, который я проверяю:

<h1 class="para-title">A quick brown fox jumps over<br>the lazy dog
<span id="something">&#xe800;</span></h1>

Вот мой код BeautifulSoap (только соответствующая часть), чтобы получить текст в тегах h1:

    soup = BeautifulSoup(page, 'html.parser')
    title_box = soup.find('h1', attrs={'class': 'para-title'})
    title = title_box.text.strip()
    print title

Это дает следующий вывод:

    A quick brown fox jumps overthe lazy dog

В то время как яожидая:

    A quick brown fox jumps over the lazy dog

Как мне заменить <br> на space в моем коде?

Ответы [ 3 ]

3 голосов
/ 09 апреля 2019

Как насчет использования .get_text() с параметром разделителя?

from bs4 import BeautifulSoup

page = '''<h1 class="para-title">A quick brown fox jumps over<br>the lazy dog
<span>some stuff here</span></h1>'''


soup = BeautifulSoup(page, 'html.parser')
title_box = soup.find('h1', attrs={'class': 'para-title'})
title = title_box.get_text(separator=" ").strip()
print (title)   

Выход:

print (title)
A quick brown fox jumps over the lazy dog
 some stuff here
2 голосов
/ 09 апреля 2019

Использование replace() в html перед анализом:

from bs4 import BeautifulSoup

html = '''<h1 class="para-title">A quick brown fox jumps over<br>the lazy dog
<span>some stuff here</span></h1>'''

html = html.replace("<br>", " ")
soup = BeautifulSoup(html, 'html.parser')
title_box = soup.find('h1', attrs={'class': 'para-title'})
title = title_box.get_text().strip()
print (title)

ВЫХОД :

A quick brown fox jumps over the lazy dog
some stuff here

РЕДАКТИРОВАТЬ :

Для части OP, упомянутой в комментариях ниже;

html = '''<div class="description">Planet Nine was initially proposed to explain the clustering of orbits
Of Planet Nine's other effects, one was unexpected, the perpendicular orbits, and the other two were found after further analysis. Although other mechanisms have been offered for many of these peculiarities, the gravitational influence of Planet Nine is the only one that explains all four.
</div>'''

from bs4 import BeautifulSoup

html = html.replace("\n", ". ")
soup = BeautifulSoup(html, 'html.parser')
div_box = soup.find('div', attrs={'class': 'description'})
divText= div_box.get_text().strip()
print (divText)

ВЫХОД :

Planet Nine was initially proposed to explain the clustering of orbits. Of Planet Nine's other effects, one was unexpected, the perpendicular orbits, and the other two were found after further analysis. Although other mechanisms have been offered for many of these peculiarities, the gravitational influence of Planet Nine is the only one that explains all four..
0 голосов
/ 09 апреля 2019

Использование str.replace Функция:
print title.replace("<br>", " ")

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...