如何使用 Selenium 和 Python 在 Instagram 关注者中向下滚动?
我假设您想要滚动到您的 Instagram 帐户的最后一个关注者。我跳过了浏览器初始化、定义等待(隐式等待)和登录的代码。所以我正在编写向下滚动到关注者的代码driver.execute_script(“arguments[0].scrollIntoView();”, 目标)目标 => 网络元素程序:-# 导航到您帐户的个人资料页面driver.get(“http://www.instagram.com/<账户名>“);# 获取所有关注者的数量totalFollowers = int(driver.find_element_by_xpath(“//a[contains(@href,’followers’)]”).text.replace(‘followers’, ”).strip())driver.find_element_by_xpath(“//a[contains(@href,’followers’)]”).click()# 循环到最后一个跟随者元素对于范围内的索引(totalFollowers): 尝试: target = driver.find_element_by_xpath(“//div[@role=’presentation’]//h1//ancestor::div/div//li[{index}]”.format(index=(indx + 1))) # 将关注者滚动到最后的代码 driver.execute_script(“arguments[0].scrollIntoView();”, 目标) 例外为 e: 继续# 打印所有账户名和用户名accountNames = driver.find_elements_by_xpath(“//div[@role=’presentation’]//a[not(contains(@style,’height’))]”)列表(地图(lambda x:打印(x.text),帐户名))userNames = driver.find_elements_by_xpath(“//div[@role=’presentation’]//button/parent::div/preceding-sibling::div//div[contains(@class,’w’)]”)”’.encode(“utf-8”) 用于处理和编码表情符号如果你不写这个,那么你会得到这种类型的例外UnicodeEncodeError:“charmap”编解码器无法编码字符”’列表(地图(lambda x:打印(x.text.encode(“utf-8”)),用户名))打印(“完成”)快乐自动化:-)(y)