如果列表中存在元素,如何从列表中删除元素

我有一个列表列表,如果元素存在于列表中,我试图从每个列表中删除一个元素。

码:

import requests
from bs4 import BeautifulSoup

# get link and parse
page = requests.get('https://www.finviz.com/screener.ashx?v=111&ft=4')
soup = BeautifulSoup(page.text, 'html.parser')

print('List of filters\n')

# return 'Title's for each filter
titles = soup.find_all('span', attrs={'class': 'screener-combo-title'})
title_list = []
for t in titles:
    title_list.append(t.contents)

print(title_list)

样本输出:

[['Price/Free Cash Flow'], ['EPS growth', <br/>, 'this year'], ['EPS growth', <br/>, 'next year']]

所需的输出:

[['Price/Free Cash Flow'], ['EPS growth', 'this year'], ['EPS growth', 'next year']]

The issue I have been running into is that my checks to see if the element is present aren't working. I have tried if '<br/>' in whatever: and whatever.remove('<br/>'). NoneType is non callable. I see that I am putting <br/> in as a string, but I also see it's not a string in the list. I have tried dropping '' and that came back unresolved reference. I have tried checking if each list has multiple elements and if so to remove the 2nd element but that also came back NoneType is non callable.

评论