在 r 中使用 for 循环进行批处理 http 状态代码测试时出错
Errors in Batch http status code test using for loop in r
我有 df
个独特的 ids x urls
。
library (httr)
for (i in (1:nrow(df))) {
resp <- httr::GET(df$url[i])
httpcode[i] <- status_code(resp)
httpstatus[i] <- http_status(resp)$reason
}
我想 (a) 为每个 url 找到 status_code
,(b) 为每个 url 找到 http_status
,以及 (c) 吐出它们进入同一个 df
.
中的新列
问题: 1. 在下面的代码中,当我将 i
替换为实际索引号(例如 i = 1)时,代码有效。当我把它放在 for 循环中时,它给了我以下错误:
Error in curl::curl_fetch_memory(url, handle = handle) :
Couldn't resolve host name
- 如何使
httpcode
和 httpstatus
从对象转换为同一 df
中的新列?谢谢
out_df <- data.frame()
for (i in df$url) {
print(i)
resp <- httr::GET(i)
httpcode <- status_code(resp)
httpstatus <- http_status(resp)$reason
row <- c(i, httpcode, httpstatus)
out_df <- rbind(out_df, row)
}
df <- merge(df, out_df, by = 'url', all.x = TRUE)
这里有一个稍微不同的方法。
首先,使用函数获取状态码和状态信息。然后使用 purrr 包的 map_df,创建一个包含 url、状态代码和消息的数据框。我使用 httr 包中的 HEAD() 函数,因为它的所有信息都在 header.
library(purrr)
## Example dataframe with a column for id and urls
urls_df <- data.frame(id = c(1, 2),
urls = c("https://www.google.gr", "https://www.google.es"),
stringsAsFactors = FALSE)
#function to get the status code and status message
status_fun <- function(my_url) {
http_head <- HEAD(my_url)
status_code_only = http_head$status_code
message = http_status(http_head)$message
data.frame(url = my_url, status_code = status_code_only, message = message)
}
# create a dataframe with the urls, status code and status message
df.new <- map_df(urls_df$urls, status_fun)
#merge the new dataframe with original
df.final <- merge(urls_df, df.new, by = 'url', all.x = TRUE)
希望对您有所帮助!
我有 df
个独特的 ids x urls
。
library (httr)
for (i in (1:nrow(df))) {
resp <- httr::GET(df$url[i])
httpcode[i] <- status_code(resp)
httpstatus[i] <- http_status(resp)$reason
}
我想 (a) 为每个 url 找到 status_code
,(b) 为每个 url 找到 http_status
,以及 (c) 吐出它们进入同一个 df
.
问题: 1. 在下面的代码中,当我将 i
替换为实际索引号(例如 i = 1)时,代码有效。当我把它放在 for 循环中时,它给了我以下错误:
Error in curl::curl_fetch_memory(url, handle = handle) :
Couldn't resolve host name
- 如何使
httpcode
和httpstatus
从对象转换为同一df
中的新列?谢谢
out_df <- data.frame()
for (i in df$url) {
print(i)
resp <- httr::GET(i)
httpcode <- status_code(resp)
httpstatus <- http_status(resp)$reason
row <- c(i, httpcode, httpstatus)
out_df <- rbind(out_df, row)
}
df <- merge(df, out_df, by = 'url', all.x = TRUE)
这里有一个稍微不同的方法。
首先,使用函数获取状态码和状态信息。然后使用 purrr 包的 map_df,创建一个包含 url、状态代码和消息的数据框。我使用 httr 包中的 HEAD() 函数,因为它的所有信息都在 header.
library(purrr)
## Example dataframe with a column for id and urls
urls_df <- data.frame(id = c(1, 2),
urls = c("https://www.google.gr", "https://www.google.es"),
stringsAsFactors = FALSE)
#function to get the status code and status message
status_fun <- function(my_url) {
http_head <- HEAD(my_url)
status_code_only = http_head$status_code
message = http_status(http_head)$message
data.frame(url = my_url, status_code = status_code_only, message = message)
}
# create a dataframe with the urls, status code and status message
df.new <- map_df(urls_df$urls, status_fun)
#merge the new dataframe with original
df.final <- merge(urls_df, df.new, by = 'url', all.x = TRUE)
希望对您有所帮助!