-3

I'm new to python (about 3 weeks). So I started learning bs4 and currently trying to tie "Xin lỗi! trang bạn tìm kiếm không tồn tại." of the HTML to a variable using bs4. I read the bs4 documentation and I got confused since english is not my primary language and also I haven't learn HTML..

<div class="title">Xin lỗi! trang bạn tìm kiếm không tồn tại.</div>

This is the coding from the source code I'm practicing on

I managed to find that part on html using the .find_all(class_="title") and thankfully only one result came out which is the one above.

So what I'm trying to do is to verify if this specific string exist in the source code, then exit the program. My idea is to assign it to a variable and if =="Xin lỗi! trang bạn tìm kiếm không tồn tại." then exit program.

If you guys have simpler idea, please do share :)

deceze
  • 510,633
  • 85
  • 743
  • 889
Nazfull
  • 11
  • 2

1 Answers1

0

.find_all

Will return all results as a list. If you know that multiple results will be found and you need to get the text for each result do the following:

from bs4 import BeautifulSoup

html  = '<div class="title">Xin lỗi! trang bạn tìm kiếm không tồn tại.</div>'

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

my_text = "Xin lỗi! trang bạn tìm kiếm không tồn tại."
title_list = soup.find_all(class_="title")

for title in title_list:
    if title.text == my_text:
        print ("text founded")

    else:
        print ("text not founded")

Or if you know that there is only result will match your search criteria you can use find()

and the code will be as the following:

from bs4 import BeautifulSoup

html  = '<div class="title">Xin lỗi! trang bạn tìm kiếm không tồn tại.</div>'

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

my_text = "Xin lỗi! trang bạn tìm kiếm không tồn tại."

text = soup.find(class_="title").text

if text == my_text:
    print ("text founded")

else:
    print ("text not founded")

Read this thread to learn more about the diffrent between find() and find_all()

Mhd O.
  • 120
  • 1
  • 8