-1

i trying check the presence of text on certain page(if you send precendently the text will appear in this zone otherwize it's blank).

html= urlopen(single_link)
parsed= BeautifulSoup.BeautifulSoup(html,'html.parser')

lastmessages = parsed.find('div',attrs={'id':'message-box'})
if lastmessages :
    print('Already Reached')
else:
     print('you can write your message')
<div class="lastMessage">
    <div class="mine messages">
        <div class="message last" id="msg-snd-1601248710299" dir="auto">
            Hello, and how are you ?
        </div>
        <div style="clear : both ;"></div>
        <div class="msg-status" id="msg-status-1601248710299" dir="rtl">
            <div class="send-state">
                Last message : &nbsp;
                <span class="r2">before 7:35 </span>
            </div>
            <div class="read-state">
                <span style="color : gray ;">&nbsp;–&nbsp;</span>
                Reading :
                &nbsp;
                <span class="r2">Not yet</span>
            </div>
        </div>
        <div style="clear : both ;"></div>
    </div>
</div>

my problem is i can't know how to find if the text "Hello, and how are you ?" exist or not ???

woblob
  • 1,349
  • 9
  • 13
yokaso75
  • 31
  • 1
  • 5
  • I don't quite understand. Do you only want to check, if text `Hello, and how are you ?` is inside the `soup`? – Andrej Kesely Sep 28 '20 at 08:17
  • i want to findout if there is a text or not, in the exemple that i sent there is a "Hello,and how are you ?" because i sent it before, but if i didn't sent it before this field will be empty. – yokaso75 Sep 28 '20 at 08:53
  • Maybe you can do `"Hello, and how are you ?" in soup.get_text()` That will return `True` if the text is present. – Andrej Kesely Sep 28 '20 at 09:00

1 Answers1

0

Simple solution

import bs4 

parsed= bs4.BeautifulSoup(html,'html.parser')
lastmessages = parsed.find('div', class_='message last')
if lastmessages :
    print(f'{lastmessages.text.strip()}')
else:
     print('No message')
woblob
  • 1,349
  • 9
  • 13
  • your approach don't solve my problem, because i want to get text inside the "div class="message last" – yokaso75 Sep 28 '20 at 10:52
  • thank you for the update, now you are understanding my problem, but still not resolve it. lastmessages = parsed.find('div', class_='message last') i already tried that line of code but i don't know why it don't reach TAG wher the text it enclosed ??? – yokaso75 Sep 28 '20 at 20:19
  • i tried regular expression to catch the id but, it didnt work : lastmessages = parsed.find(id=re.compile('^msg-snd-')) – yokaso75 Sep 29 '20 at 06:51