BeautifulSoup并从列表中删除结果?蟒蛇

时间:2012-12-02 08:22:07

标签: python list beautifulsoup

我正在尝试从我从一些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个,所以也有一些。

谢谢你们

1 个答案:

答案 0 :(得分: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]
相关问题