【发布时间】:2019-07-12 01:33:16
【问题描述】:
我需要将上面网页中的美国检查/崩溃表解析为 R 数据框。适用于网站上某些表格的解析技术不适用于其他表格。
我能够使用以下代码解析检查表:
inspections <- carrier %>%
html_node('.querylabel+ center table') %>%
html_table(fill = TRUE)
但是当我尝试解析检查表正下方的崩溃表时,我得到了错误:
Error in UseMethod("html_table") :
no applicable method for 'html_table' applied to an object of class
"xml_missing"
我使用了以下代码:
crashes <- carrier %>%
html_node('center:nth-child(19) table') %>%
html_table(fill = TRUE)
我使用选择器小工具来选择那个表的 css 是 'center:nth-child(19) table'。我还尝试使用带有 x 路径的 html_node():
crashes <- carrier %>%
html_node(xpath = '//center[(((count(preceding-sibling::*) + 1) =
19) and parent::*)]//table') %>%
html_table(fill = TRUE)
那也没用。我对网络抓取很陌生,所以如果这是一个简单的解决方案,我深表歉意。
运营商是网址:
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)
【问题讨论】:
标签: r web-scraping rvest