将抓取的数据导出到一个CSV

时间:2018-10-08 08:05:35

标签: r web-scraping rvest rbind

我设法在R(rvest)中收集了选举信息,但现在我在为如何将数据保存在单独的 CSV文件中而不是在< em> CSV文件。

这是我的工作代码,可在其中分别剪贴11、12、13页。

library(rvest)
library(xml2)

do.call(rbind, lapply(11:13, 
                  function(n) {
url <- paste0("http://www.cvk.gov.ua/pls/vnd2014/WP040?PT001F01=910&pf7331=", n)
mi <- read_html(url)%>% html_table(fill = TRUE)
mi[[8]]

file <- paste0("election2014_", n, ".csv")
if (!file.exists(file)) write.csv(mi[[8]], file) 
Sys.sleep(5)
}))

我最终尝试这样做,但是它没有按预期工作

write.csv(rbind(mi[[8]],url), file="election2014.csv") 

2 个答案:

答案 0 :(得分:-1)

我无法使您的代码正常工作,但是您可以在运行代码之前尝试创建一个空的数据框,然后在编写包含完整数据的csv文件之前执行此操作:

df   = rbind(df,mi[[8]])

您还可以考虑使用purrr软件包将csv文件转换为一个文件:

files = list.files("folder_name",pattern="*.csv",full.names = T)

df = files %>%
  map(read_csv) %>%
  reduce(rbind)

答案 1 :(得分:-1)

尝试这个:

from PyQt5.QtGui import QTextCursor

# set the cursor position to 0
cursor = QTextCursor(textEdit.document())
# set the cursor position (defaults to 0 so this is redundant)
cursor.setPosition(0)
textEdit.setTextCursor(cursor)

# insert text at the cursor
textEdit.insertPlainText('your text here')
相关问题