0

I'm getting a string by scraping a certain website, then I'm trying to check if the first value of that string (in this case it's " <h2>maanantai 10.1.</h2> " contains the letter 'a'. If it does (like it does) - print 'YES'.

For some reason which I don't understand it just doesn't work.

import urllib.request
from bs4 import BeautifulSoup

url = 'https://kouluruoka.fi/menu/kouvola_koulujenruokalista'

request = urllib.request.Request(url)
content = urllib.request.urlopen(request)

parse = BeautifulSoup(content, 'html.parser')

h2_elements = parse.find_all('h2')

first_value_in_string = h2_elements[1]

# this one prints " <h2>maanantai 10.1.</h2> " as it should
print(first_value_in_string)

# this one should check if the value (first_value_in_string) contains the letter 'a' and if it does then print 'YES' but for some reason it doesnt
if 'a' in first_value_in_string:
    print('YES')
2
  • 2
    'a' in " <h2>maanantai 10.1.</h2> " returns True, so are you sure about your debugging, for example is first_value_in_string actually a string? Commented Jan 13, 2022 at 17:52
  • Does this answer your question? How to get a html text inside tag using BeautifulSoup Commented Jan 13, 2022 at 18:02

1 Answer 1

3

first_value_in_string data type is <class 'bs4.element.Tag'> so cast it to string when you compare or better get the text attribute:

if 'a' in first_value_in_string.text:
    print('YES')

or when you assign it :

first_value_in_string = h2_elements[1].text

alternatively you can use .string attribute

Sign up to request clarification or add additional context in comments.

1 Comment

You can also do first_value_in_string.string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.