user3030473
user3030473

Reputation: 77

List Append Conditionals

In this specific situation, How do I make a proper if conditional that appends only a price value for example below 53 to a list?

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)

The above code outputs:

[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']
[...]

However print a outside the for loop prints only one value obviously because it did only one search instead of a loop trough the all the offers.

Upvotes: 0

Views: 201

Answers (1)

WeaselFox
WeaselFox

Reputation: 7380

Assuming your regex works properly, something like this would probably do:

for price  in a:
    if int(price)<=53:
        price_list.append(price)
        offers_list2.append(price)

Also, DO NOT PARSE HTML WITH REGEX

Upvotes: 2

Related Questions