使用splashr进行Web抓取时出现许多错误,包括执行_lua(splash_obj,call_函数)中的错误:网关超时(HTTP 504)
我正在尝试使用使用splashr进行Web抓取时出现许多错误,包括执行_lua(splash_obj,call_函数)中的错误:网关超时(HTTP 504),r,web-scraping,rvest,http-status-code-504,R,Web Scraping,Rvest,Http Status Code 504,我正在尝试使用splashr来抓取动态网页,这对我来说是个不停的问题。在我的get\u box\u score() Error in execute_lua(splash_obj, call_function) : Gateway Timeout (HTTP 504). 或 老实说,一旦我“修复”了其中一个错误,我就得到了另一个。我不知道这些是相关的,还是我的代码中有很多不同的不相关的错误。你知道我该怎么修吗?这是我的密码: library(tidyverse) library(splash
splashr
来抓取动态网页,这对我来说是个不停的问题。在我的get\u box\u score()
Error in execute_lua(splash_obj, call_function) :
Gateway Timeout (HTTP 504).
或
老实说,一旦我“修复”了其中一个错误,我就得到了另一个。我不知道这些是相关的,还是我的代码中有很多不同的不相关的错误。你知道我该怎么修吗?这是我的密码:
library(tidyverse)
library(splashr)
library(rvest)
url <- "https://www.uscho.com/scoreboard/michigan/mens-hockey/"
# Everything should be fine for a while
get_data <- function(myurl) {
link_data <- myurl %>%
read_html() %>%
html_nodes("td:nth-child(13) a") %>%
html_attr("href") %>%
str_c("https://www.uscho.com", .) %>%
as_tibble() %>%
set_names("url")
game_type <- myurl %>%
read_html() %>%
html_nodes("td:nth-child(12)") %>%
html_text() %>%
as_tibble() %>%
set_names("game_type") %>%
filter(game_type != "Type")
as_tibble(data.frame(link_data, game_type))
}
link_list <- get_data(url)
urls <- link_list %>%
filter(game_type != "EX") %>%
pull(url)
# Here's where the fun starts
get_box_score <- function(my_url) {
progress_bar$tick()$print()
Sys.sleep(15)
splash_container <- start_splash()
on.exit(stop_splash(splash_container))
Sys.sleep(10)
mydata <- splash_local %>%
splash_response_body(TRUE) %>%
splash_user_agent(ua_win10_chrome) %>%
splash_go(my_url) %>%
splash_wait(runif(1, 5, 10)) %>%
splash_html() %>%
html_node("#boxgoals") %>%
html_table(fill = TRUE) %>%
as_tibble()
return(mydata)
}
progress_bar <- link_list %>%
filter(game_type != "EX") %>%
tally() %>%
progress_estimated(min_time = 0)
mydata <- pmap_df(list(urls), get_box_score)
库(tidyverse)
图书馆(splashr)
图书馆(rvest)
url%
html_attr(“href”)%%>%
str_c(“https://www.uscho.com", .) %>%
as_tible()%>%
设置名称(“url”)
游戏类型%
读取html()%>%
html_节点(“td:n子节点(12)”)%>%
html_text()%>%
as_tible()%>%
设置游戏名称(“游戏类型”)%>%
过滤器(游戏类型!=“类型”)
as_tible(data.frame(链接数据,游戏类型))
}
链接列表%
拉(url)
#这就是乐趣的开始
获得\u盒\u分数%
splash_go(我的url)%>%
splash_wait(runif(1,5,10))%>%
splash_html()%>%
html#U节点(“#boxgoals”)%>%
html_表格(fill=TRUE)%>%
作为_tible()
返回(mydata)
}
进度百分比
过滤器(游戏类型!=“EX”)%>%
计数()%>%
估计进度(最小时间=0)
mydata您的代码没有问题。504是服务器端错误;如果服务器无法及时处理请求,则可能会发生错误
但您仍然可以通过调整代码来修复它;您可以尝试以下方法:
<> L>>P>减慢你的请求,使用<代码> sys·睡眠()/代码>在每个请求之间暂停,如果请求太快,服务器可能无法处理它,并且可能认为你喜欢机器人并禁止你。因此,您将收到504错误
- 使用
try()
或tryCatch()。您还可以编写代码,在请求失败时自动尝试发出另一个请求
library(tidyverse)
library(splashr)
library(rvest)
url <- "https://www.uscho.com/scoreboard/michigan/mens-hockey/"
# Everything should be fine for a while
get_data <- function(myurl) {
link_data <- myurl %>%
read_html() %>%
html_nodes("td:nth-child(13) a") %>%
html_attr("href") %>%
str_c("https://www.uscho.com", .) %>%
as_tibble() %>%
set_names("url")
game_type <- myurl %>%
read_html() %>%
html_nodes("td:nth-child(12)") %>%
html_text() %>%
as_tibble() %>%
set_names("game_type") %>%
filter(game_type != "Type")
as_tibble(data.frame(link_data, game_type))
}
link_list <- get_data(url)
urls <- link_list %>%
filter(game_type != "EX") %>%
pull(url)
# Here's where the fun starts
get_box_score <- function(my_url) {
progress_bar$tick()$print()
Sys.sleep(15)
splash_container <- start_splash()
on.exit(stop_splash(splash_container))
Sys.sleep(10)
mydata <- splash_local %>%
splash_response_body(TRUE) %>%
splash_user_agent(ua_win10_chrome) %>%
splash_go(my_url) %>%
splash_wait(runif(1, 5, 10)) %>%
splash_html() %>%
html_node("#boxgoals") %>%
html_table(fill = TRUE) %>%
as_tibble()
return(mydata)
}
progress_bar <- link_list %>%
filter(game_type != "EX") %>%
tally() %>%
progress_estimated(min_time = 0)
mydata <- pmap_df(list(urls), get_box_score)