You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

同一爬虫代码对不同行数据返回结果不一致问题咨询

Airbnb评论数抓取不稳定问题排查与解决

核心原因分析

抓取结果时灵时不灵,本质是动态页面加载不充分、Airbnb反爬机制拦截,再加上可能使用了不稳定的元素选择器三者共同作用的结果。

针对性解决办法

1. 确保页面完全加载后再抓取

Airbnb的评论数属于动态渲染内容,RSelenium默认不会等待所有元素加载完成就执行抓取,这是最常见的"空值/NA"诱因:

  • 用waitForElement等待目标元素出现,替代固定的Sys.sleep()(更可靠):
    # 以中文页面为例,等待包含"条评价"的元素加载,超时10秒
    webDriver$waitForElement(using = "xpath", value = "//span[contains(text(), '条评价')]", timeout = 10000)
    
  • 若评论区需要滚动页面才加载,模拟人类滚动行为:
    # 滚动到页面底部触发加载
    webDriver$executeScript("window.scrollTo(0, document.body.scrollHeight);")
    Sys.sleep(2) # 给动态内容留加载缓冲时间
    

2. 优化元素选择器,规避动态class

Airbnb的前端class多为动态生成(比如_17bkx6h这类随机命名),每次页面加载可能变化,直接导致选择器失效:

  • 改用基于文本或固定结构的XPath,比如中文页面定位包含"条评价"的span:
    review_count <- read_html(page_source) %>%
      html_element(xpath = "//span[contains(text(), '条评价')]") %>%
      html_text() %>%
      stringr::str_extract("\\d+") # 提取纯数字部分
    
  • 英文页面只需将'条评价'替换为'reviews'即可。

3. 规避反爬机制,模拟人类行为

Airbnb会检测自动化请求,轻则限制内容加载,重则直接拦截:

  • 加入随机延迟,避免固定间隔触发反爬:
    Sys.sleep(runif(1, 2, 5)) # 每次请求等待2-5秒随机时长
    
  • 自定义浏览器User-Agent,伪装成真实用户:
    eCaps <- list(chromeOptions = list(
      args = c('--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 <- rsDriver(browser = "chrome", extraCapabilities = eCaps, port = sample(4444:4450, 1))
    
  • 定期重启浏览器,避免会话被标记为异常:
    # 每抓取20个房源重启一次浏览器
    if (i %% 20 == 0) {
      webDriver$close()
      driver$server$stop()
      Sys.sleep(3)
      driver <- rsDriver(browser = "chrome", extraCapabilities = eCaps, port = sample(4444:4450, 1))
      webDriver <- driver$client
    }
    

4. 加入异常捕获,避免批量失败

用tryCatch包裹抓取逻辑,单个房源失败时返回NA并记录错误,不影响整体任务执行:

scrape_reviews <- function(url) {
  tryCatch({
    webDriver$navigate(url)
    # 等待评论元素加载
    webDriver$waitForElement(using = "xpath", value = "//span[contains(text(), '条评价')]", timeout = 8000)
    page_source <- webDriver$getPageSource()[[1]]
    # 提取评论数
    count <- read_html(page_source) %>%
      html_element(xpath = "//span[contains(text(), '条评价')]") %>%
      html_text() %>%
      stringr::str_extract("\\d+") %>%
      as.integer()
    return(count)
  }, error = function(e) {
    message(paste("URL:", url, "抓取失败,原因:", e$message))
    return(NA_integer_)
  })
}

# 批量调用示例
df$review_count <- lapply(df$listing_url, scrape_reviews) %>% unlist()

额外检查点

  • 确认评论数是否在iframe中:若页面存在嵌套iframe,需先切换到iframe再抓取:
    iframe <- webDriver$findElement(using = "css", value = "iframe[title='评论']")
    webDriver$switchToFrame(iframe)
    # 之后再执行元素定位逻辑
    
  • 检查网络稳定性:不稳定的网络会导致页面加载中断,直接出现空值或NA。

内容的提问来源于stack exchange,提问作者shubham tiwari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 00:41:11