Python Forum

Full Version: Working on my FOR loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello friends!

Actually I'm a little new on phyton and I been working on a scrape file; in a part on code is like:

isver = paged_soup.find_all('span',
{'class': 'a-size-mini a-color-state a-text-bold'}, href=True)
isver = [ifv.text for ifv in isver]
If it all is right, this build a 10 items vector and isver must have a value like:

[Verified Purchase, Verified Purchase, Verified Purchase, Verified Purchase, Verified Purchase, Verified Purchase, Verified Purchase, Verified Purchase, Verified Purchase, Verified Purchase]


But the problem come when the page have less than 10 Verified Purchase items, these make a short vectors. For example: if it get 2 items found the vector is like: [Verified Purchase, Verified Purchase]
But I always looking to build a 10 item vector like:
[No Verified Purchase, Verified Purchase, No Verified Purchase, No Verified Purchase, Verified Purchase, No Verified Purchase, No Verified Purchase, No Verified Purchase, No Verified Purchase, No Verified Purchase]
(since I don't know many Verified Purchase will found, the order can be variable)

Them I try change the code at something like this:
                            rcounter=0
                            for r in isver:
                                rcounter = rcounter + 1						
                                if (r == '<span class="a-size-mini a-color-state a-text-bold" data-hook="avp-badge">Verified Purchase</span>'):
                                    if (rcounter <= 10):
                                        isver = [isver.append(r.text)]
                                else:
                                    if (rcounter <=10):
                                        r = 'No Verified Purchase'
                                        isver = [isver.append(r)]
This not build any vector at all and everytime I print r the value is : <span class="a-size-mini a-color-state a-text-bold" data-hook="avp-badge">Verified Purchase</span>

But it doesn't match on If contidion and do directly here.
else:
       if (rcounter <=10):
       r = 'No Verified Purchase'
       isver = [isver.append(r)]
And also r value is replace to all For loop.

What can I do in that case?, I'll appreciate some idea;
Thanks in advance.

Wilson Rivas.
#!/usr/bin/python3
import random

#create array with random length
isver = [ "Verfified Purchase" ] * random.randint(0,10)
print(len(isver), isver)
print()

#now we want always ten elements
isver += ["Verified Purchase New"] * (10 - len(isver))
print(len(isver), isver)