r语言 - 需要帮助解释错误:"read_xml.raw"



我正在学习本教程RSelenium,在测试navigation_click((函数之前,"全部"都能正常工作。(集合名称与教程不同,因为我的源网站不同。(

navigate_click <- function() {
webElem <- remDr$findElement(using = "class name",
"google-visualization-table-div-page")
Sys.sleep(0.5)
webElem$clickElement()
remDr$getPageSource()[[1]] %>% 
read_xml() %>%
xml_ns_strip() %>%
xml_find_all(xpath = '//td') %>%
xml_text() %>%
set_names(c("PublicationTitle", "County", "Place_of_Publication", "Library")) %>%
as.list() %>% as_tibble()
}

它返回一个错误:

读取_xml.raw(charToRaw(enc2utf8(x((时出错,"UTF-8"。。。,as_html=as_html,:xmlParseEntityRef:无名称[68]

这是Traceback。。。

> navigate_click()
Error in read_xml.raw(charToRaw(enc2utf8(x)), "UTF-8", ..., as_html = as_html,  : 
xmlParseEntityRef: no name [68] 
11. read_xml.raw(charToRaw(enc2utf8(x)), "UTF-8", ..., as_html = as_html, 
options = options) 
10. read_xml.character(.) 
9. read_xml(.) 
8. function_list[[i]](value) 
7. freduce(value, `_function_list`) 
6. `_fseq`(`_lhs`) 
5. eval(quote(`_fseq`(`_lhs`)), env, env) 
4. eval(quote(`_fseq`(`_lhs`)), env, env) 
3. withVisible(eval(quote(`_fseq`(`_lhs`)), env, env)) 
2. remDr$getPageSource()[[1]] %>% read_xml() %>% xml_ns_strip() %>% 
xml_find_all(xpath = "//td") %>% xml_text() %>% set_names(c("PublicationTitle", 
"County", "Place_of_Publication", "Library")) %>% as.list() %>% 
as_tibble() 
1. navigate_click() 

我发现您正在查看的博客有点令人费解;我不清楚navigate_click函数是如何工作的,因为它使用HTML源代码并调用read_xml()。虽然有些HTML页面可能符合严格的XML格式,但大多数都不是格式良好的XML。在这些情况下,read_xml将抛出一个错误。

幸运的是,xml2包还有一个read_html函数,它可以毫无问题地解析页面。然而,这不会修复您的函数,因为当您挑选出td元素并获得它们的文本内容时,您将获得一个无法应用set_names的单个字符向量。

在任何情况下,rvest包使从解析的html中读取表变得更加容易。

假设您已经按照您的示例完成了install.packages("rvest")并创建了remDr,那么以下操作应该有效:

remDr$navigate("https://view-awesome-table.com/-Lz90gtPDhIyGUzmdMrE/view")
webElem <- remDr$findElement(using = "class name", "google-visualization-table-div-page")
Sys.sleep(0.5)
webElem$clickElement()
remDr$getPageSource()[[1]] %>%
read_html(x) %>% 
xml_find_all(xpath = "//*[@class = 'google-visualization-table-table']") %>%
rvest::html_table() %>%
`[[`(1) %>%
`[`(c(1, 2, 3, 7)) %>%
as_tibble()
#> # A tibble: 15 x 4
#>    PublicationTitle              County    Place_of_Publicati~ Library               
#>    <chr>                         <chr>     <chr>               <chr>                 
#>  1 ALFRETON AND DISTRICT ADVERT~ Derbyshi~ "Alfreton and Ripl~ British Library       
#>  2 ALFRETON AND DISTRICT ADVERT~ Derbyshi~ "Alfreton and Ripl~ Derbyshire: County Ha~
#>  3 ALFRETON AND DISTRICT COMING~ Derbyshi~ "Alfreton"          British Library       
#>  4 ALFRETON AND DISTRICT COMING~ Derbyshi~ "Alfreton"          Derbyshire: County Ha~
#>  5 ALFRETON AND DISTRICT ECHO    Derbyshi~ "Alfreton"          British Library       
#>  6 ALFRETON AND DISTRICT ECHO    Derbyshi~ "Alfreton"          Derbyshire: County Ha~
#>  7 ALFRETON AND RIPLEY ECHO      Derbyshi~ "Chesterfield"      British Library       
#>  8 ALFRETON AND RIPLEY ECHO      Derbyshi~ "Chesterfield"      Derbyshire: Alfreton  
#>  9 ALFRETON ARGUS                Derbyshi~ "Alfreton"          British Library       
#> 10 ALFRETON ARGUS                Derbyshi~ "Alfreton"          Derbyshire: County Ha~
#> 11 ALFRETON JOURNAL              Derbyshi~ ""                  British Library       
#> 12 ALFRETON JOURNAL              Derbyshi~ ""                  Derbyshire: Alfreton  
#> 13 ALFRETON JOURNAL              Derbyshi~ ""                  Derbyshire: County Ha~
#> 14 ALFRETON JOURNAL              Derbyshi~ ""                  Derbyshire: Magic Att~
#> 15 ALFRETON TRADER               Derbyshi~ ""                  British Library       

最新更新