【问题标题】:How to extract id names from search result urls using rvest? (CSS selector isn't working)如何使用 rvest 从搜索结果 url 中提取 id 名称? (CSS 选择器不工作)
【发布时间】:2018-05-15 17:56:42
【问题描述】:

我正在尝试从搜索结果页面 (link here) 中提取产品项目名称列表。

library(rvest)
results <- read_html('https://www.fishersci.com/us/en/catalog/search/products?keyword=sodium+hydroxide&nav=')
results %>%
  html_nodes(".result_title a") %>%
  html_text()

返回

character(0)

我也尝试过使用:

html_attr('href')

没有运气。我什至可以使用 css 来提取这些链接的标题吗?我正在尝试列出 30 个产品结果(例如“氢氧化钠(颗粒/认证 ACS),Fisher Chemical”)。这些链接的 id 是否使用 javascript?

感谢您的帮助,这是我的第一个抓取项目,我的网页设计知识比这个特定页面简单得多。

【问题讨论】:

    标签: css r web-scraping rvest


    【解决方案1】:

    结果确实是用javascript生成的。 rvest 目前不处理 javascript,但存在其他替代方案。

    例如,您可以使用seleniumphantomjs 来获得您想要的:

    library(RSelenium) # Wrapper around Selenium
    library(wdman)     # helper to download and configure phantomjs
    library(rvest)
    
    phantomjs <- phantomjs(port = 4444L)
    remote_driver <- remote_driver(browserName = "phantomjs", port = 4444L)
    remote_driver <- remoteDriver(browserName = "phantomjs", port = 4444L)
    remote_driver$open(silent = TRUE)
    remote_driver$navigate("https://www.fishersci.com/us/en/catalog/search/products?keyword=sodium+hydroxide&nav=")
    remote_driver$getPageSource()[[1]]
    
    page_source %>% 
      read_html() %>% 
      html_nodes(css = '.result_title') %>% 
      html_text()
    

    【讨论】:

      猜你喜欢
      • 2015-08-31
      • 2019-09-20
      • 1970-01-01
      • 1970-01-01
      • 2018-02-05
      • 2018-04-23
      • 1970-01-01
      • 2020-06-27
      • 2018-12-06
      相关资源
      最近更新 更多