Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Working on my FOR loop
#1
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.
Reply
#2
#!/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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  while loop not working-I am using sublime text editor mma_python 4 1,060 Feb-05-2023, 06:26 PM
Last Post: deanhystad
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,434 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  Why is the while loop not working? mcoliver88 5 3,044 Aug-18-2020, 03:27 PM
Last Post: deanhystad
  Infinite loop not working pmp2 2 1,609 Aug-18-2020, 12:27 PM
Last Post: deanhystad
  Loop not working Nonameface 8 2,834 Jul-19-2020, 12:27 PM
Last Post: snippsat
  for loop script over telnet in Python 3.5 is not working abhijithd123 1 2,860 May-10-2020, 03:22 AM
Last Post: bowlofred
  How to keep a loop containing a web import working in python executable? coder1384 3 2,821 Feb-22-2020, 06:49 PM
Last Post: snippsat
  For loop not working pjgrah01 4 2,758 Nov-03-2019, 11:58 PM
Last Post: pjgrah01
  Appending to list not working and causing a infinite loop eiger23 8 3,943 Oct-10-2019, 03:41 PM
Last Post: eiger23
  plotting inside the loop is not working jenya56 4 3,035 Apr-10-2019, 08:11 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020