简体   繁体   中英

Python BeautifulSoup check output

I try to check what a website prints out. So imagine:

page = requests.get("testsite.com")
soup = BeautifulSoup(page.content)
if soup in ["my", "wished", "output"]:
    print "It's there!"
else:
    print "It's not there!"

So I want to check if the website is containing the text "my", "wished" or "output". How do I do that?

Thank you!

page = requests.get("testsite.com")
test = ["my", "wished", "output"]
if any(t in page.text for t in test):
    print("It's there!")
else:
    print("It's not there!")

soup is BeautifulSoup object, if you want to check if text in string, you should use string object like page.text

any(iterable) Return True if any element of the iterable is true. If the iterable is empty, return False.

You can use map function and to find strings in page . If there is neither of the wanted string in the page , returned list will contain only -1 (ie. [-1, -1, -1] ). So, just check if there is number that is bigger than -1 .

page = requests.get("testsite.com")
page = page.text
wanted = ["my", "wished", "output"]
if max(map(page.find, wanted)) > -1:
    print "It's there!"
else:
    print "It's not there!"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM