在测试navigate_click()函数之前,我遵循本教程RSelenium与刮擦所有的工作原理。( 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: no name 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() 发布于 2020-04-28 11:35:26
我发现您正在查看的博客有点令人费解;我不清楚navigate_click函数如何工作,因为它使用了一个read_xml()源代码并在其上调用了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 https://stackoverflow.com/questions/61431313
复制相似问题