需要帮助将带有rvest的表解析为dataframe吗

需要帮助将带有rvest的表解析为dataframe吗,r,web-scraping,rvest,R,Web Scraping,Rvest,我需要从上面的网页将美国检查/崩溃下的表解析为R数据框。对站点上的某些表有效的解析技术对其他表无效 我能够用以下代码解析检查表: inspections <- carrier %>% html_node('.querylabel+ center table') %>% html_table(fill = TRUE) crashes <- carrier %>% html_node('center:nth-chil

我需要从上面的网页将美国检查/崩溃下的表解析为R数据框。对站点上的某些表有效的解析技术对其他表无效

我能够用以下代码解析检查表:

    inspections <- carrier %>%
      html_node('.querylabel+ center table') %>%
      html_table(fill = TRUE)
    crashes <- carrier %>%
      html_node('center:nth-child(19) table') %>%
      html_table(fill = TRUE)
我使用了以下代码:

    inspections <- carrier %>%
      html_node('.querylabel+ center table') %>%
      html_table(fill = TRUE)
    crashes <- carrier %>%
      html_node('center:nth-child(19) table') %>%
      html_table(fill = TRUE)
崩溃%
html_节点('中心:第n个子(19)表')%>%
html_表(fill=TRUE)
我使用选择器小工具来选择css,该表的css是“中心:第n个子(19)表”。我还尝试将html_node()与x路径一起使用:

    crashes <- carrier %>%
      html_node(xpath = '//center[(((count(preceding-sibling::*) + 1) = 
     19) and parent::*)]//table') %>%
      html_table(fill = TRUE)
崩溃%
html_节点(xpath='//center[((计数(前面的同级::*)+1)=
19) 和父项::*)]//表“')%>%
html_表(fill=TRUE)
那也没用。我对网络垃圾很陌生,所以如果这是一个简单的解决方案,我很抱歉

carrier是url:

    carrier <- read_html(https://safer.fmcsa.dot.gov/query.asp?searchtype=ANY&query_type=queryCarrierSnapshot&query_param=USDOT&original_query_param=NAME&query_string=2249709&original_query_string=ARKANSAS%20BEST%20LOGISTICS%20INC)
carrier有两个“检查”表和两个“碰撞”表,美国和加拿大各一个。这里有两种方法:

  • 使用前面的链接(“检查:”、“碰撞:”)来识别链接后面的
    中心
    元素。然后查找
    节点,并对其进行解析

  • 您也可以使用
    a[href$='#InspectionsCA'].
    为加拿大执行此操作,但格式不理想(“崩溃:”表具有相同的
    href
    值)。(请注意,
    href$=
    表示链接以该文本结尾:)


  • 使用表的
    summary
    字段获取两组“Inspections”和“crasks”表,并命名它们(例如使用
    purr::set_names
    c(“美国”、“加拿大”)
    ),或者丢弃您不想要的表(使用
    [
    ):


  • 非常好的答案+@gizaom如果答案非常有效,那么您应该通过单击✓. 这样别人也会知道什么对你有用。
                  Inspection Type Vehicle Driver Hazmat IEP
    1                 Inspections       0      0      0   0
    2              Out of Service       0      0      0   0
    3            Out of Service %      0%     0%     0%  0%
    4 Nat'l Average %(2009- 2010)  20.72%  5.51%  4.50% N/A
    
    dot_url %>% 
      html_node("a[href$='#Accidents'] + center") %>% 
      html_node("table") %>% 
      html_table()
    
         Type Fatal Injury Tow Total
    1 Crashes     0      0   0     0
    
    dot_url %>% 
      html_nodes("table[summary='Inspections']") %>% 
      html_table()
    
    [[1]]
                  Inspection Type Vehicle Driver Hazmat IEP
    1                 Inspections       0      0      0   0
    2              Out of Service       0      0      0   0
    3            Out of Service %      0%     0%     0%  0%
    4 Nat'l Average %(2009- 2010)  20.72%  5.51%  4.50% N/A
    
    [[2]]
       Inspection Type Vehicle Driver
    1      Inspections       0      0
    2   Out of Service       0      0
    3 Out of Service %      0%     0%
    
    dot_url %>% 
      html_nodes("table[summary='Crashes']") %>% 
      html_table()
    
    [[1]]
         Type Fatal Injury Tow Total
    1 Crashes     0      0   0     0
    
    [[2]]
         Type Fatal Injury Tow Total
    1 Crashes     0      0   0     0