【问题标题】:Need help interpreting an error: "read_xml.raw"需要帮助解释错误:“read_xml.raw”
【发布时间】:2020-04-25 19:23:53
【问题描述】:

我正在关注本教程RSelenium and scraping 在我开始测试 navigate_click() 函数之前一切正常。 (set_names 与教程不同,因为我的源网站不同。)

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()
}

它返回一个错误:

read_xml.raw(charToRaw(enc2utf8(x)), "UTF-8", ..., as_html = as_html, 中的错误: xmlParseEntityRef: 没有名字 [68]

这是回溯...

> 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() 

【问题讨论】:

标签: r xml rselenium


【解决方案1】:

我发现您正在查看的博客有点令人费解;我不清楚 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       

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多