在这种特定情况下,我如何制作一个合适的if条件,仅将例如低于53的价格值附加到列表中?
offers_list = re.findall("<div class=\"list\"(.*?)</div>", http_response_body, re.DOTALL) # Find list of offers in HTTP Response Body
price_list = []
offers_list2 = []
for i in offers_list: # loop in the list of offers to look for the specific price values
a = re.findall("\"price\"=(.*?)\"", i) # Find specific price value within in each offer
print a
price_list.append(a) # Append to list only if the price is lower than X amount
offers_list2.append(a)
以上代码输出:
[u'47.00']
[u'49.00']
[u'49.00']
[u'50.00']
[u'50.00']
[u'50.00']
[u'50.00']
[u'51.50']
[u'52.50']
[u'53.00']
[...]
然而print a
之外的for loop
只显示一个值,因为它只通过所有商品只进行了一次搜索而不是循环。
答案 0 :(得分:2)
假设你的正则表达式正常工作,可能会这样:
for price in a:
if int(price)<=53:
price_list.append(price)
offers_list2.append(price)