Scrolling web page using selenium python webdriver

后端 未结 1 1778
不知归路
不知归路 2020-12-06 19:40

I am scraping this webpage for usernames which loads the users after scrolling

Url to page : \"http://www.quora.com/Kevin-Rose/followers\"

I know the number

相关标签:
1条回答
  • 2020-12-06 20:24

    Since there is nothing special appearing after the last followers bucket is loaded, I would rely on the fact that you know how many followers does the user have and you know how many are loaded on each scroll down (I've inspected - it is 18 per scroll). Hence, you can calculate how many times do you need to scroll the page down.

    Here's the implementation (I've used a different user with only 53 followers to demonstrate the solution):

    import time
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    followers_per_page = 18
    
    driver = webdriver.Chrome()  # webdriver.Firefox() in your case
    driver.get("http://www.quora.com/Andrew-Delikat/followers")
    
    # get the followers count
    element = WebDriverWait(driver, 2).until(EC.presence_of_element_located((By.XPATH, '//li[contains(@class, "FollowersNavItem")]//span[@class="profile_count"]')))
    followers_count = int(element.text.replace(',', ''))
    print followers_count
    
    # scroll down the page iteratively with a delay
    for _ in xrange(0, followers_count/followers_per_page + 1):
        driver.execute_script("window.scrollTo(0, 10000);")
        time.sleep(2)
    

    Also, you may need to increase this 10000 Y coordinate value based on the loop variable in case there is a big number of followers.

    0 讨论(0)
提交回复
热议问题