我試圖將聯合國安理會(UNSC)決議的文本劃分爲R.聯合國維護PDF格式(here)的所有UNSC決議的聯機存檔。所以,從理論上講,這應該是可行的。將iframe中的PDF刮入R
如果我點擊特定年份的超鏈接,然後點擊特定文檔的鏈接(例如,this one),我可以在瀏覽器中看到PDF。當我嘗試通過在URL欄中的鏈接指向download.file
來下載該PDF時,它似乎可行。但是,當我嘗試使用pdftools
程序包中的pdf_text
函數將該文件的內容讀入R時,我收到了一堆錯誤消息。
這是我試圖失敗的。如果你運行它,你會看到我正在談論的錯誤消息。
library(pdftools)
pdflink <- "http://www.un.org/en/ga/search/view_doc.asp?symbol=S/RES/2341(2017)"
tmp <- tempfile()
download.file(pdflink, tmp, mode = "wb")
doc <- pdf_text(tmp)
我錯過了什麼?我想認爲它與這些文件的可下載版本的鏈接地址不同,這些地址與瀏覽器內顯示的鏈接地址不同,但我無法弄清楚如何獲得前者的路徑。我試圖右鍵點擊下載圖標;使用Chrome中的「檢查」選項查看標識爲'src'的URL(this link);並指出我的過程的其餘部分。 download.file
部分再一次執行,但運行pdf_text
時收到相同的錯誤消息。我還嘗試了a)將mode
的mode
部分改爲download.file
,並且b)將「.pdf」加到tmp
的路徑的末尾,但這兩者都沒有幫助。
什麼錯誤信息你好嗎?你可以用'download.file()'下載文件後打開文件嗎? – MrFlick
@MrFlick,錯誤消息的堆棧以'錯誤:可能不是PDF文件(繼續)'開頭,包含一堆「非法字符」消息,並以'錯誤:PDF解析失敗'結尾。 – ulfelder
我試過了您的代碼和下載的PDF文件已損壞。當我嘗試瀏覽器中的鏈接時,它也會出現一些錯誤。鏈接是否正確? – anonR