Selenium WebDriver无法识别部分Twitter账号的粉丝数元素
解决Twitter粉丝数爬取部分账号失败的问题
核心原因分析
- 隐私账号限制:部分账号设置为私人账号,未登录状态下无法查看粉丝数,页面DOM结构完全不同,自然找不到目标元素。
- 绝对XPath不可靠:你用的是全路径绝对XPath,Twitter页面用React动态渲染,不同账号类型、登录状态甚至页面版本都会导致DOM层级变化,绝对XPath一换页面就失效。
- 反爬机制触发:Twitter检测到自动化工具访问,可能返回简化版页面、触发人机验证,或者隐藏交互元素,导致无法定位。
- 页面加载不充分:隐式等待是全局等待,但某些元素可能需要更长时间加载,或者需要等待特定渲染状态完成。
针对性解决方案
1. 替换为可靠的相对XPath
放弃绝对路径,基于元素的语义属性定位,比如粉丝数对应的链接通常包含/followers路径,且有明确的文本标识:
# 定位包含/followers的链接下的数字span follower_xpath = '//a[contains(@href, "/followers")]//span[contains(@class, "css-1jxf684") and not(contains(text(), "Followers"))]'
或者更精准的,通过aria-label筛选:
follower_xpath = '//a[@aria-label and contains(@aria-label, "followers")]//span[text()]'
2. 处理隐私/受限账号
在爬取前先检查页面是否有私人账号提示:
try: private_notice = driver.find_element(By.XPATH, '//div[contains(text(), "This account is private")]') print(f"账号{acc}是私人账号,无法获取粉丝数") continue except: pass
3. 模拟真实浏览器,规避反爬
添加更多Chrome选项,减少自动化特征:
options = webdriver.ChromeOptions() options.add_argument("--disable-notifications") # 禁用自动化检测 options.add_argument("--disable-blink-features=AutomationControlled") options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) # 设置真实UA options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36")
4. 使用显式等待代替隐式等待
显式等待可以精准等待目标元素加载完成,比全局隐式等待更可靠:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # 等待20秒,直到元素出现 wait = WebDriverWait(driver, 20) follower_element = wait.until(EC.presence_of_element_located((By.XPATH, follower_xpath))) f = follower_element.text print(f"账号{acc}的粉丝数:{f}")
5. 登录Twitter(可选但有效)
对于私人账号或反爬严格的情况,登录后可以访问更多内容,添加登录步骤:
# 进入登录页 driver.get("https://twitter.com/login") # 等待并输入用户名 username_input = wait.until(EC.presence_of_element_located((By.NAME, "text"))) username_input.send_keys("你的账号") # 点击下一步,后续步骤同理输入密码完成登录
修改后的完整代码示例
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import ChromeService from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC acc = "目标账号" url = f"https://twitter.com/{acc}" options = webdriver.ChromeOptions() options.add_argument("--disable-notifications") options.add_argument("--disable-blink-features=AutomationControlled") options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36") driver = webdriver.Chrome(service=ChromeService(), options=options) wait = WebDriverWait(driver, 20) try: driver.get(url) # 检查是否为私人账号 try: wait.until(EC.presence_of_element_located((By.XPATH, '//div[contains(text(), "This account is private")]'))) print(f"账号{acc}是私人账号,无法获取粉丝数") except: # 定位粉丝数元素 follower_xpath = '//a[contains(@href, "/followers")]//span[contains(@class, "css-1jxf684") and not(contains(text(), "Followers"))]' follower_element = wait.until(EC.presence_of_element_located((By.XPATH, follower_xpath))) print(f"账号{acc}的粉丝数:{follower_element.text}") except Exception as e: print(f"爬取账号{acc}出错:{str(e)}") finally: driver.quit()
内容的提问来源于stack exchange,提问作者s p
相关产品推荐
相关产品推荐

