简体   繁体   中英

I have a while loop that's being ignored even though it shouldn't | Selenium | Python

I've created a small demo to demonstrate on a smaller scale what I'm trying to do with my web scraper. What my web scraper does is go to an account on Instagram, scrape the last few posts on it (the number of posts scanned depends on the post_limit variable), the problem arises when the code is supposed to move on to other accounts listed in the account_list list. Instead, it just ignores the while loop

while len(account_list) > account_picker_order:"

except for the first time, why would it do this if there are still accounts that still have to be scanned??? It doesn't even go to the 2nd account's page, let alone scan its posts. What's going on here? Please help as I've been scratching my head for days:(

def imgoinginsanepleasehelpmefixthis():

    #chooses which account is picked from the list
    account_picker_order = -1

    driver = webdriver.Chrome("/Users/apple/Downloads/chromedriver")
    
    post_index = 0

    #sets the limit for posts scanned
    post_limit = 3

    #list of target accounts
    account_list = ["xtechnation", "technologyreview", "techcrunch", "technology", "itechexplore", "tech.dailys", "blemmtech", "technologyhive", "rising.tech"]

    #cycles through all accounts and their posts before stopping (at least it's supposed to)
    while len(account_list) > account_picker_order:

        account_picker_order += 1
        post_index = 0

        if account_picker_order == 0:

            #to stop browser from opeining new window with each scan
            driver.get('https://instagram.com')
            
            sleep(3)

        #fetches the appropriate account
        driver.get('https://instagram.com/' + account_list[account_picker_order] + '/')

        sleep(3)

        while post_index != post_limit:
        
        #pretend this is the part where the posts are checked
        #this usually cycles through multiple posts, or a single post, depending on the limit

        post_index += 1

imgoinginsanepleasehelpmefixthis()

You shuld be using a for loop instead of a while loop:

def imgoinginsanepleasehelpmefixthis():

    driver = webdriver.Chrome("/Users/apple/Downloads/chromedriver")
    driver.get('https://instagram.com')
    post_limit = 3
    account_list = ["xtechnation", "technologyreview", "techcrunch", "technology", "itechexplore", "tech.dailys", "blemmtech", "technologyhive", "rising.tech"]
    for account in account_list:
        driver.get(f'https://instagram.com/{account}')
        sleep(3)
        for post in range(post_limit):
            print("fake post check")


and you shuld probably be passing both account_list and post limit as an argument to the function like this:

def imgoinginsanepleasehelpmefixthis(post_limit, account_list):

    driver = webdriver.Chrome("/Users/apple/Downloads/chromedriver")
    driver.get('https://instagram.com')
    for account in account_list:
        driver.get(f'https://instagram.com/{account}')
        sleep(3)
        for post in range(post_limit):
            print("fake post check")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM