使用Python滾動Instagram關注者彈出視窗


Instagram是一個流行的社交媒體平臺,允許使用者與關注者聯絡和分享內容。作為開發者,可能需要自動化Instagram上的某些任務,例如提取關注者資料。Instagram的關注者彈出視窗每次只加載有限數量的關注者,使用者需要向下滾動才能檢視更多關注者。在本文中,我們將探討如何使用Python滾動Instagram關注者彈出視窗。

語法

webdriver.Chrome('path/to/chromedriver

此方法用於建立Chrome WebDriver的例項。它需要提供chromedriver可執行檔案的路徑作為引數。

driver.get(url)

此方法用於在網路瀏覽器中導航到指定的URL。它以URL作為引數,並載入相應的網頁。

element = driver.find_element_by_name(name)

此方法用於根據其name屬性在網頁上查詢HTML元素。它返回一個表示已找到元素的WebElement物件。

element.send_keys(*value)

此方法用於模擬鍵盤輸入到輸入欄位或元素中。它以輸入值作為引數,並將其輸入到指定的元素中。

wait = WebDriverWait(driver, timeout)

此類用於在Selenium中設定顯式等待。它以WebDriver例項和最大等待時間作為引數。

分步實施

步驟1:匯入和設定環境:我們首先需要安裝必要的依賴項,包括Python、selenium和相應的webdriver,並在你的Python指令碼中匯入這些庫。可以使用Python包管理器pip安裝這些庫。你可以如下匯入所需的庫

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

步驟2:初始化WebDriver:要初始化web驅動程式,需要建立一個webdriver.Chrome的例項,並提供Chrome驅動程式可執行檔案的路徑。你可以如下初始化ChromeDriver

from selenium import webdriver

driver = webdriver.Chrome('path/to/chromedriver')

步驟3:導航到Instagram登入頁面:使用driver.get()方法可以導航到Instagram登入頁面。導航到頁面後,使用Selenium的find_element_by_name方法找到使用者名稱和密碼輸入欄位。

# Navigate to Instagram's login page
driver.get('https://www.instagram.com/accounts/login/')

# Wait for the page to load
time.sleep(2)

# Find the username and password input fields, and fill in the login credentials
username_input = driver.find_element_by_name('username')
password_input = driver.find_element_by_name('password')

步驟4:填寫登入憑據:使用find_element_by_name()方法找到使用者名稱和密碼輸入欄位後,可以使用相應輸入欄位上的send_keys()方法將登入憑據傳遞給輸入欄位。

username_input.send_keys('your_username')  # Replace 'your_username' with your Instagram username
password_input.send_keys('your_password')  # Replace 'your_password' with your Instagram password

步驟5:提交登入表單:傳遞憑據後,現在可以透過呼叫密碼輸入欄位上的submit()方法提交登入表單。

# Submit the login form
password_input.submit()

步驟6:導航到使用者的個人資料頁面:登入成功後,可以使用WebDriver的get()方法導航到使用者的個人資料頁面。將'your_username'替換為你的Instagram使用者名稱。你還可以使用time.sleep()方法等待頁面完全載入。

# Wait for the login process to complete
time.sleep(5)

# Navigate to the user's profile page
driver.get('https://www.instagram.com/your_username')  # Replace 'your_username' with your Instagram username

# Wait for the profile page to load
time.sleep(2)

步驟7:點選關注者按鈕:可以使用find_element_by_xpath()方法和XPath選擇器在個人資料頁面上找到關注者按鈕。WebDriverWait類用於等待頁面上出現該元素。然後使用click()方法點選關注者按鈕。

# Find and click on the followers button
followers_button = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, '//a[@href="/your_username/followers/"]'))
)  # Replace 'your_username' with your Instagram username
followers_button.click()

步驟8:等待關注者彈出視窗載入:我們使用WebDriverWait等待關注者彈出窗口出現在頁面上。然後使用find_element_by_xpath()方法和XPath選擇器找到關注者彈出視窗。

# Wait for the followers popup to load
followers_popup = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, '//div[@class="isgrP"]'))
)

步驟9:滾動關注者彈出視窗:定義一個JavaScript指令碼將關注者彈出視窗滾動到底部。該指令碼將元素的scrollTop屬性設定為其scrollHeight,這會將其滾動到最大高度。我們使用while迴圈重複執行滾動指令碼,直到沒有載入新的關注者。在迴圈中,我們跟蹤滾動前後關注者的數量。如果關注者的數量保持不變,則表示沒有載入新的關注者,我們退出迴圈。

# Scroll down the followers popup
scroll_script = "arguments[0].scrollTop = arguments[0].scrollHeight;"
while True:
    last_count = len(driver.find_elements_by_xpath('//div[@class="isgrP"]//li'))
    driver.execute_script(scroll_script, followers_popup)
    time.sleep(1)  # Add a delay to allow time for the followers to load
    new_count = len(driver.find_elements_by_xpath('//div[@class="isgrP"]//li'))
    if new_count == last_count:
        break  # Exit the loop if no new followers are loaded

步驟10:關閉網頁瀏覽器

Call the quit() method on the WebDriver to close the web browser and free system resources.

輸出

結論

在本文中,我們討論瞭如何使用Python中的Selenium滾動Instagram關注者彈出視窗。透過利用Python和Selenium WebDriver,我們可以高效地滾動Instagram上的關注者彈出視窗並提取關注者資料。在本文中,我們討論了分步過程,並提供了示例程式碼片段來幫助你入門。

更新於:2023年7月18日

1000+ 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.