Python BeautifulSoup: получение текста из тега div - PullRequest
1 голос
/ 07 января 2020

Я новичок ie в поиске в сети. Я использую красивый суп для извлечения Google Play Store. Тем не менее, я застрял, чтобы получить текст из тега Div. Тег Div выглядит следующим образом:

a = <`div class="LVQB0b"><div class="QoPmEb"></div><div><span class="X43Kjb">Education.com</span><span class="p2TkOb">August 15, 2019</span></div>Thanks for your feedback. We are sorry to hear you're having trouble with the app. This is a known issue and our team has fixed it. Please restart the app and let us know at support@education.com if you have any further trouble. Thanks!</div>` 

Я хочу получить текст, начиная с «Спасибо за ваш отзыв». Я использовал следующий код для извлечения текста:

response = a.find('div',{'class':'LVQB0b'}).get_text()

Однако вышеприведенная команда также возвращает нежелательный текст, т.е. «Education.com» и дату. Я не уверен, как извлечь текст из тега div, у которого нет имени класса, как показано выше в примере. Жду вашего руководства.

Ответы [ 3 ]

3 голосов
/ 07 января 2020

Использование find(text=True, recursive=False)

Пример:

from bs4 import BeautifulSoup

s = '''<div class="LVQB0b"><div class="QoPmEb"></div><div><span class="X43Kjb">Education.com</span><span class="p2TkOb">August 15, 2019</span></div>Thanks for your feedback. We are sorry to hear you're having trouble with the app. This is a known issue and our team has fixed it. Please restart the app and let us know at support@education.com if you have any further trouble. Thanks!</div>'''    
html = BeautifulSoup(s, 'html.parser')
print(html.find('div',{'class':'LVQB0b'}).find(text=True, recursive=False))

Выход:

Thanks for your feedback. We are sorry to hear you're having trouble with the app. This is a known issue and our team has fixed it. Please restart the app and let us know at support@education.com if you have any further trouble. Thanks!
2 голосов
/ 07 января 2020

Нежелательный текст является частью элемента <div class="LVQB0b">. Вы можете найти эти элементы и удалить их текст из результата

response = a.find('div',{'class':'LVQB0b'}).get_text()
unwanted = a.select('.LVQB0b span')
for el in unwanted:
    response = response.replace(el.get_text(), '')
1 голос
/ 07 января 2020

В качестве альтернативы Вы можете использовать next_sibling или find_next_sibling(text=True)

from bs4 import BeautifulSoup

html= '''<div class="LVQB0b"><div class="QoPmEb"></div><div><span class="X43Kjb">Education.com</span><span class="p2TkOb">August 15, 2019</span></div>Thanks for your feedback. We are sorry to hear you're having trouble with the app. This is a known issue and our team has fixed it. Please restart the app and let us know at support@education.com if you have any further trouble. Thanks!</div>'''
soup = BeautifulSoup(html, 'html.parser')
print(soup.find('div',class_='QoPmEb').find_next('div').next_sibling)

from bs4 import BeautifulSoup

html= '''<div class="LVQB0b"><div class="QoPmEb"></div><div><span class="X43Kjb">Education.com</span><span class="p2TkOb">August 15, 2019</span></div>Thanks for your feedback. We are sorry to hear you're having trouble with the app. This is a known issue and our team has fixed it. Please restart the app and let us know at support@education.com if you have any further trouble. Thanks!</div>'''
soup = BeautifulSoup(html, 'html.parser')
print(soup.find('div',class_='QoPmEb').find_next('div').find_next_sibling(text=True))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...