如何在解析器(python)中替换或删除无

时间:2015-03-06 12:45:16

标签: python beautifulsoup

当我使用BeautifulSoup解析网站时,我得到:

None
offer_15542518
None
None
None
None
None
None
None
None
None
None

代码:

soup = BeautifulSoup(html)
projects =[]
table = soup.find('table', class_='objects_items_list')
for row in table.find_all('tr'):
    internal_id = row.get('id')
    print( internal_id )

如何摆脱None?我只需要id。

1 个答案:

答案 0 :(得分:1)

soup = BeautifulSoup(html)
projects =[]
table = soup.find('table', class_='objects_items_list')
for row in table.find_all('tr'):
    internal_id = row.get('id')
    if internal_id is not None:
        print( internal_id )

最简单的方法是检查internal_id是否为无,只有在不是时才打印出来。