【发布时间】:2021-06-15 22:40:45
【问题描述】:
我是网络抓取的新手,我已经成功地为多种不同类型的网站编写了函数,这些函数将我想要的信息收集到一个数据框中。然而,话虽如此,这些功能是在 Mac 上使用 RSelenium 开发的。尝试在我的 Windows PC 上运行完全相同的功能时,它们会失败。
我认为这个问题与 RSelenium 的启动方式有关。下面是我如何在每个不同的网络抓取功能中启动 RSelenium:
rs <- rsDriver(browser = "firefox", port = netstat::free_port())
remote <- rs$client
remote$navigate(url)
在函数中执行此 R 代码时,我在将 Firefox 指定为 RSelenium 使用的浏览器时收到以下错误:
Could not open firefox browser.
Client error message:
Undefined error in httr call. httr output: Failed to connect to localhost port 14415: Connection refused
Check server log for further details.
Error in checkError(res) :
Undefined error in httr call. httr output: length(url) == 1 is not TRUE
In addition: Warning message:
In rsDriver(browser = "firefox", port = netstat::free_port()) :
Error in checkError(res) :
Undefined error in httr call. httr output: length(url) == 1 is not TRUE
这是我在将 Google Chrome 指定为 RSelenium 使用的浏览器时收到的错误:
Could not open chrome browser.
Client error message:
Undefined error in httr call. httr output: Failed to connect to localhost port 14415: Connection refused
Check server log for further details.
Error in checkError(res) :
Undefined error in httr call. httr output: length(url) == 1 is not TRUE
In addition: Warning message:
In rsDriver(browser = "chrome", port = netstat::free_port()) :
Error in checkError(res) :
Undefined error in httr call. httr output: length(url) == 1 is not TRUE
同样,这些错误仅出现在 Windows PC 环境中,而不出现在我的 Mac 上。
为了尝试解决这个问题,我搜索了 Stack Overflow 以寻找可能的答案。我已经切换了要使用的端口,例如 4444L,但无济于事。它会导致同样的错误。
在我的 Windows PC 上运行 sessionInfo() 时,它会产生以下输出:
R version 4.1.0 (2021-05-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] netstat_0.1.1 assertthat_0.2.1 jsonlite_1.7.2 webdriver_1.0.6 V8_3.4.2 RSelenium_1.7.7 rvest_1.0.0
[8] lubridate_1.7.10 forcats_0.5.1 stringr_1.4.0 dplyr_1.0.6 purrr_0.3.4 readr_1.4.0 tidyr_1.1.3
[15] tibble_3.1.2 ggplot2_3.3.3 tidyverse_1.3.1
loaded via a namespace (and not attached):
[1] Rcpp_1.0.6 binman_0.1.2 png_0.1-7 ps_1.6.0 utf8_1.2.1 showimage_1.0.0 R6_2.5.0
[8] cellranger_1.1.0 backports_1.2.1 reprex_2.0.0 httr_1.4.2 pillar_1.6.1 rlang_0.4.11 curl_4.3.1
[15] readxl_1.3.1 rstudioapi_0.13 callr_3.7.0 wdman_0.2.5 munsell_0.5.0 broom_0.7.6 compiler_4.1.0
[22] modelr_0.1.8 janitor_2.1.0 pkgconfig_2.0.3 askpass_1.1 base64enc_0.1-3 openssl_1.4.4 tidyselect_1.1.1
[29] XML_3.99-0.6 fansi_0.5.0 crayon_1.4.1 dbplyr_2.1.1 withr_2.4.2 bitops_1.0-7 grid_4.1.0
[36] gtable_0.3.0 lifecycle_1.0.0 DBI_1.1.1 magrittr_2.0.1 semver_0.2.0 scales_1.1.1 debugme_1.1.0
[43] cli_2.5.0 stringi_1.6.2 fs_1.5.0 snakecase_0.11.0 xml2_1.3.2 ellipsis_0.3.2 generics_0.1.0
[50] vctrs_0.3.8 tools_4.1.0 glue_1.4.2 hms_1.1.0 processx_3.5.2 colorspace_2.0-1 caTools_1.18.2
[57] haven_2.4.1
如何让 RSelenium 跨设备工作?
【问题讨论】:
标签: r web-scraping rselenium