在Instagram上使用Python中的Selenium取消关注用户,可以通过以下步骤实现:
pip install selenium
来安装Selenium。from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome() # 使用Chrome浏览器,需要下载对应的驱动程序
driver.get("https://www.instagram.com/")
username = "YourUsername"
password = "YourPassword"
input_username = driver.find_element_by_name("username")
input_username.send_keys(username)
input_password = driver.find_element_by_name("password")
input_password.send_keys(password)
login_button = driver.find_element_by_xpath("//button[@type='submit']")
login_button.click()
search_input = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//input[@placeholder='Search']"))
)
search_input.send_keys("UsernameToUnfollow") # 要取消关注的用户名
search_input.send_keys(Keys.RETURN)
user_profile = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//a[@href='/usernameToUnfollow/']"))
)
user_profile.click()
unfollow_button = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//button[contains(text(), 'Following')]"))
)
unfollow_button.click()
confirm_unfollow_button = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//button[contains(text(), 'Unfollow')]"))
)
confirm_unfollow_button.click()
通过以上步骤,使用Python中的Selenium可以在Instagram上取消关注用户。请注意,上述代码仅供参考,具体的网页元素定位可能因Instagram网页结构的变化而需要调整。
领取专属 10元无门槛券
手把手带您无忧上云