【发布时间】:2019-03-05 13:19:59
【问题描述】:
我最近编写了一个脚本,以便通过 API 调用查找搜索量。
这是我使用的脚本:
install.packages("SEMrushR")
library(SEMrushR)
#Data frame to append data
final_result_useo_rumbo <- data.frame()
mes_keywords_to_check <- readLines("useo_rumbo_es.txt")
mes_keywords_to_check <- as.character(mes_keywords_to_check)
#Loop in order to look for each keyword that is in my list, then return Search volume thanks to the API call and finally store it in a new database.
for (i in 1:length(mes_keywords_to_check)) {
test_keyword <- as.character(mes_keywords_to_check[i])
df_test_2 <- keyword_overview_all(test_keyword, "es","API KEY")
final_result_useo_rumbo <- rbind(final_result_useo_rumbo,df_test_2)
}
脚本运行良好,但问题是我有很多关键字要检查 (800 000)。 当我为 60 000 个关键字做这件事时,花了将近 4 个小时才完成......
你知道我可以如何加快这个过程吗?有没有更好的方法来编写脚本?
【问题讨论】:
-
我们可以重写您的脚本以使用更优化的 R 代码,但我怀疑这是否需要 4 小时缩短到几分钟。
-
您可以进行的一项更改是避免在
for循环中使用rbind。它会导致二次复制。 -
@TimBiegeleisen 删除迭代
rbind可以很容易地将它从几小时缩短到几分钟,我敢打赌 -
@HongOoi 然后将其发布为答案。
-
@HongOoi 应该用什么来代替 rbind?