0

我正在尝试从我从一些 BeautifulSoup 结果创建的列表中删除项目。我的 for 循环似乎删除了一些符合我在 if 和 elif 条件中的条件的项目——但不是全部。你们能帮帮我吗?

from BeautifulSoup import BeautifulSoup
import urllib
import re

# url constructor for search
pass
pass
pass

# read the site into a string
site = urllib.urlopen("http://losangeles.craigslist.org/moa/")
html = site.read()
site.close()
soup = BeautifulSoup(html)



#Price
prices = soup.findAll("span", {"class":"itempp"})
prices = [str(j).strip('<span class="itempp"> $</span>') for j in prices]
print prices
print len(prices)

for k in prices:
    if k == '':
        prices.remove(k)
    elif int(k) <= 50:
        prices.remove(k)

print prices
print len(prices)

返回:

['230', '120', '1', '333', '180', '380', '120', '1', '230', '25', '300', '500', '480', '199', '600', '180', '260', '200', '35', '250', '10', '80', '200', '20', '20', '', '20', '15', '10', '', '35', '225', '9', '', '79', '280', '30', '10', '10', '80', '1', '20', '', '15', '20', '200', '550', '20', '700', '350', '25', '30', '99', '', '', '40', '', '200', '25', '20', '180', '', '120', '50', '700', '120', '380', '320', '130', '325', '175', '180', '120', '260', '5', '450', '650', '400', '700', '50', '600', '250', '699', '245', '649', '450', '615', '450', '665', '5', '5', '50', '1100', '30', '300', '700', '15', '12', '12', '10']
100
['230', '120', '333', '180', '380', '120', '230', '300', '500', '480', '199', '600', '180', '260', '200', '250', '80', '200', '35', '225', '79', '280', '10', '10', '80', '20', '20', '200', '550', '20', '700', '350', '99', '', '', '200', '20', '180', '', '120', '700', '120', '380', '320', '130', '325', '175', '180', '120', '260', '450', '650', '400', '700', '600', '250', '699', '245', '649', '450', '615', '450', '665', '5', '1100', '30', '300', '700', '15', '12', '10']
71

**请注意,第一个 1 已被删除,但随后还保留了一些 10,一些 ' 也是如此。

多谢你们

4

1 回答 1

1

prices如果您想在循环期间删除某些项目,您应该遍历列表的副本。尝试:

for k in prices[:]:
    if k == '':
        prices.remove(k)
    elif int(k) <= 50:
        prices.remove(k)

或使用列表推导:

prices[:] = [p for p in prices if p and int(p) > 50]
于 2012-12-02T08:26:14.427 回答