2015-11-04 88 views
0

就像標題所說的那樣,我有一個元標記,我需要刮掉一些信息。 Link 這是從這個網站我試圖提取作者從屬關係。而這些信息,我可以找到與這行代碼的使用谷歌的開發工具:在我需要在javascript中提取的元標記中提供信息

document.getElementsByName('citation_author_institution') 

然後我回到了我認爲是一個元素或節點的數組,看起來像這樣:

[<meta name=​"citation_author_institution" content=​"Columbia University, New York">​, <meta name=​"citation_author_institution" content=​"Columbia University, New York">​, <meta name=​"citation_author_institution" content=​"Columbia University, New York">​] 

現在我需要訪問內容並將其保存爲一個數組,以便將其放入我的數據庫中。 我曾嘗試過的東西,如

document.getElementsByName('citation_author_institution').textContent 
document.getElementsByName('citation_author_institution').getAttribute('content') 

但這並不奏效。任何人有任何想法或提示我如何做到這一點?

+0

http://stackoverflow.com/questions/3289302/can-i-get-a-meta-value-with-jquery-js –

+0

我會用的getElementsByTagName開始,真的。理論上,HTML中可能有更多的元素具有相同的名稱。 –

回答

2

你非常接近。什麼,你需要做的是通過節點列表進行迭代:

var elements = document.getElementsByName('citation_author_institution') 
    var contents = [] 

    for (var i = 0; i < elements.length; i++) { 
     contents.push(elements[i].content); 
    } 

    console.log(contents) 

所以,contents將是你的內容列表。例如這裏 https://jsfiddle.net/o3Lzm4ca/

+0

但如果我這樣做,並不會打印出數組中的所有內容?我的意思是這樣的: meta name =「citation_author_institution」content =「哥倫比亞大學,紐約」>, anderssinho

+0

@anderssinho更新您的問題以顯示您正在運行的確切代碼和確切的輸出。仔細閱讀Luis的代碼以瞭解數組的內容。該數組不包含(整個)元素,也不包含它的HTML表示。 – dsh

1
var authors = []; 
var elements = document.getElementsByName('citation_author_institution'); 
for (var i=0; i<elements.length; i++){ 
    authors.push(elements[i].content); 
} 
console.log(authors) 
+0

但如果我這樣做,並不會打印出陣列中的所有內容?我的意思是這樣的: meta name =「citation_author_institution」content =「哥倫比亞大學,紐約」>, anderssinho