How do i validate and assign my output in a simple way

Viewed 67

Is it possible to do something like this in python:

I have a single 'span' that I want to scrape.

I just want to simplify how I get the data, I will get an error if I convert the bs4 instance to text if it is None. and in general it would be nice if python had some kind of functionality like this. :-)

note = x.text for event.find('span', {'class': 'header-3'}) as x if not None else ''

Thank you in advance!

Edit:

So far i solved my issue with bs4 by making a function that i can apply on all my bs4 instances:

get_text = lambda x: x.text.strip() if x is not None else ''
note = get_text(event.find('span', {'class': 'header-3'}))

But it would still be nice to know if there was a nice way like the one in my first codeblock.

1 Answers

Is this what you are looking for?

event = BeautifulSoup(response.text, 'html.parser')
note = event.findAll('span', {'class': 'header-3'})
Related