2016-05-16 55 views
0

我有以下的HTML代碼,我想解析(一些元素被剝離,以提高可讀性):使用如何解析不良結構的html代碼?

document.getElementsByClassName("article-detail-description")[0].textContent

</div> 
      <article class="article-detail-description"> 
       <h1 class="page-heading"> 
        Postulat operacyjności definicji w naukach społecznych 
        <br /><small>Definition’s Operativeness Postulate in Social Sciences</small> 
       </h1> 

       <div> 
        <strong>Author(s): </strong>Jakub Karpiński<br /><strong>Subject(s): </strong>Social Sciences<br /><strong>Published by: </strong>Instytut Filozofii i Socjologii Polskiej Akademii Nauk<br/><strong>Keywords: </strong>operationism; definition of property; definition of indicator; concepts selection 
<br/> 
       </div> 
        <p class="summary"><strong>Summary/Abstract: </strong> 
The article’s primary goal is to demonstrate the problems inherited in 「operationism – antioperationism」 polemics. 
</p> 
       <ul class="nav nav-tabs"> 
        <li class="active" ><a href="#details" data-toggle="tab">Details</a></li> 

        <li><a href="#tableOfContents" data-toggle="tab">Contents</a></li> 

       </ul> 
       <div class="tab-content"> 
        <div class="tab-pane fade active in" id="details"> 
         <p class="journal-link"><strong>Journal: </strong><a href="/search/journal-detail?id=10">Studia Socjologiczne</a></p> 
         <ul class="article-additional-info"> 
          <li><strong>Issue Year:</strong> 2011</li><li><strong>Issue No:</strong> 1 (200)</li><li><strong>Page Range:</strong> 65-80</li><li><strong>Page Count:</strong> 15</li><li><strong>Language:</strong> Polish</li> 
         </ul> 
        </div> 

我能讀懂它的全部。

爲只讀<p class="summary"我用:

getElementsByClassName("summary")[0].textContent

然而,後者是不完美的,因爲它也表明Summary/Abstract:

我感興趣的許多元素,讓我們下面的例子:

。 PostulatoperacyjnościdefinicjiW¯¯naukachspołecznych

我可以得到:

Postulat operacyjności definicji w naukach społecznych 
Definition’s Operativeness Postulate in Social Sciences 

爲了得到它,我用:document.getElementsByClassName("page-heading")[0].innerText

如何獲得單獨Postulat operacyjności definicji w naukach społecznychDefinition’s Operativeness Postulate in Social Sciences

。我想獲得例如2011來自:

`<li><strong>Issue Year:</strong> 2011</li><li>` 

這時候,我沒有任何線索,以獲取這些信息。 Issue No:等等也是如此。

+1

你沒有任何解析,您正在閱讀的DOM元素的屬性。 – RobG

+0

關於你所描述的內容,也沒有什麼「不健全的」。 HTML節點可以有子節點和子文本。這並不會導致格式不正確或非結構化。 – Jamiec

+0

可能的解決方案在[這裏]描述(http://stackoverflow.com/questions/9955955/get-text-of-an-element-without-children-in-javascript) –

回答

0

這取決於結構是否穩定;但你可以去訪問文本節點:

var heading = document.getElementsByClassName('page-heading')[0]; 
 
var polish = heading.childNodes[0].textContent.trim(); 
 
var english = heading.childNodes[2].textContent.trim(); 
 
console.log("Polish:", polish); 
 
console.log("English:", english); 
 

 
var li = document.querySelector('.article-additional-info li'); 
 
var issueYear = li.childNodes[1].textContent.trim(); 
 
console.log("Issue Year:", issueYear);
</div> 
 
      <article class="article-detail-description"> 
 
       <h1 class="page-heading"> 
 
        Postulat operacyjności definicji w naukach społecznych 
 
        <br /><small>Definition’s Operativeness Postulate in Social Sciences</small> 
 
       </h1> 
 

 
       <div> 
 
        <strong>Author(s): </strong>Jakub Karpiński<br /><strong>Subject(s): </strong>Social Sciences<br /><strong>Published by: </strong>Instytut Filozofii i Socjologii Polskiej Akademii Nauk<br/><strong>Keywords: </strong>operationism; definition of property; definition of indicator; concepts selection 
 
<br/> 
 
       </div> 
 
        <p class="summary"><strong>Summary/Abstract: </strong> 
 
The article’s primary goal is to demonstrate the problems inherited in 「operationism – antioperationism」 polemics. 
 
</p> 
 
       <ul class="nav nav-tabs"> 
 
        <li class="active" ><a href="#details" data-toggle="tab">Details</a></li> 
 

 
        <li><a href="#tableOfContents" data-toggle="tab">Contents</a></li> 
 

 
       </ul> 
 
       <div class="tab-content"> 
 
        <div class="tab-pane fade active in" id="details"> 
 
         <p class="journal-link"><strong>Journal: </strong><a href="/search/journal-detail?id=10">Studia Socjologiczne</a></p> 
 
         <ul class="article-additional-info"> 
 
          <li><strong>Issue Year:</strong> 2011</li><li><strong>Issue No:</strong> 1 (200)</li><li><strong>Page Range:</strong> 65-80</li><li><strong>Page Count:</strong> 15</li><li><strong>Language:</strong> Polish</li> 
 
         </ul> 
 
        </div>