Skip to content
Advertisement

How do I filter HTML elements in Python

Ive got a list of strings by scraping a website. I want the code to print the HTML elements from that list IF they contain “L” in them. Ive managed to write a code that works just fine on “normal list” that I manually just write into the code (example 1 below) but as soon as I try using that code to filter the list of HTML elements it only prints empty “[]” even though I know there should be multiple values.

Here is the code that works:

JavaScript

But as soon as I use my web scraping list (span_elements) insteas of a list of hellos, it prints blank:

JavaScript

Ive been trying for hours and got nowhere, help is appreciated! Thank you!

Advertisement

Answer

The elements in filtered_list are not strings but bs4 element objects. If you change your filter to convert them to str before using in, the code works:

JavaScript

if you want only the inside of the <span> use .text:

JavaScript

I have never used bs4 but the clue was in printing the original list:

JavaScript

output:

JavaScript

this is not a list of strings, no ' in sight.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement