Получение данных класса из BeautifulSoup - PullRequest
0 голосов
/ 27 мая 2020

Я пытаюсь получить данные класса со страницы HTML с помощью BeautifulSoup. Вот как выглядят данные:

    <div class="quoteText">
      &ldquo;I'm selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can't handle me at my worst, then you sure as hell don't deserve me at my best.&rdquo;
  <br>  &#8213;
  <span class="authorOrTitle">
    Marilyn Monroe
  </span>
</div>

Мне просто нужны данные в классе «quoteText» без данных в классе «authorOrTitle»

Следующий скрипт возвращает имя автор.

for div in soup.find('div', {'class': 'quoteText'}):
    print(div)

Как мне получить данные класса "quoteText" без данных класса "authorOrTitle"?

Спасибо!

1 Ответ

1 голос
/ 27 мая 2020

попробуйте это,

from bs4 import BeautifulSoup

sample = """<div class="quoteText">
      &ldquo;I'm selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can't handle me at my worst, then you sure as hell don't deserve me at my best.&rdquo;
  <br>  &#8213;
  <span class="authorOrTitle">
    Marilyn Monroe
  </span>
</div>
"""

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

print(soup.find('div', {'class': 'quoteText'}).contents[0].strip())
...