【发布时间】:2019-01-05 03:44:57
【问题描述】:
我在抓取网页时遇到了一些困难。具体来说,我正在抓取通常嵌入表格的网页。但是,对于没有嵌入表的实例,我似乎无法以不破坏循环的方式处理错误。
示例代码如下:
event = c("UFC 226: Miocic vs. Cormier", "ONE Championship 76: Battle for the Heavens", "Rizin FF 12")
eventLinks = c("https://www.bestfightodds.com/events/ufc-226-miocic-vs-cormier-1447", "https://www.bestfightodds.com/events/one-championship-76-battle-for-the-heavens-1532", "https://www.bestfightodds.com/events/rizin-ff-12-1538")
testLinks = data.frame(event, eventLinks)
for (i in 1:length(testLinks)) {
print(testLinks$event[i])
event = tryCatch(as.data.frame(read_html(testLinks$eventLink[i]) %>% html_table(fill=T)),
error = function(e) {NA})
}
第二个链接没有嵌入表格。我以为我会用我的 tryCatch 跳过它,但链接没有跳过它,而是打破了循环。
我希望找出一种方法来跳过没有表格的链接,但继续抓取列表中的下一个链接。要继续使用上面的示例,我希望 tryCatch 从第二个链接移动到第三个链接。
有什么帮助吗?非常感谢!
【问题讨论】:
标签: r web-scraping try-catch rvest