Dynamic Script
Dynamic Script

Reputation: 29

Print function in python not working with web scraping

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')

Upvotes: 0

Views: 168

Answers (1)

eshirvana
eshirvana

Reputation: 24568

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

Upvotes: 3

Related Questions