【问题标题】:How to get values of select menu?如何获取选择菜单的值?
【发布时间】:2019-05-17 12:05:32
【问题描述】:

我正在尝试获取this webpage 上选择菜单的值(所有区域)。我有什么错?几乎尝试了所有组合,但结果为零。其中之一是:

  page <- read_html("https://www.yemeksepeti.com/en/istanbul")
  regions <- page %>% 
    html_nodes("div") %>% 
    html_nodes("span") %>% 
    html_nodes(xpath = '//*[@id="select2-ys-areaSelector-container"]') %>% 
    html_attr("title")

提前致谢。

【问题讨论】:

  • 你试过'//*[@id="ys-areaSelector"]'吗?
  • 不是零,而是NA。

标签: html r web-scraping rvest


【解决方案1】:

XPath 是一种丑陋的野兽。获取 select 元素的 id,然后获取所有选项组,最后获取它们的文本数据。使用html_text 将其转换为R character

page <- read_html("https://www.yemeksepeti.com/en/istanbul")
regions <- page %>% 
  html_nodes(xpath='//*[@id="ys-areaSelector"]/optgroup/*/text()') %>%
  html_text()

【讨论】:

    【解决方案2】:

    假设所有选项值都需要,我会使用 css 选择器组合

    library(rvest)
    page <- read_html("https://www.yemeksepeti.com/en/istanbul")
    options <- page %>% 
      html_nodes('#ys-areaSelector [data-url]') %>%
      html_text()
    

    【讨论】:

      猜你喜欢
      • 2021-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      相关资源
      最近更新 更多