2016-02-17 51 views
1

我做了一個HTML請求,我想從中檢索特定元素,但我不知道如何使用BeautifulSoup4訪問它們。BeautifulSoup4中的訪問值

這裏是返回的HTML的示例:

<td valign="top" > 
    <span class="recordAttribute" >Taxonomy</span>: Mollusca, Gastropoda, Littorinimorpha, Hydrobiidae, Hydrobia<br> 
    <span class="recordAttribute" >Identifiers:</span> AF118324[sampleid]    <br> 
    <span class="recordAttribute" >Depository</span>: Mined from GenBank, NCBI     &nbsp; 
</td> 

我想訪問元素AF118324(這是標識符跨度類後的名稱)。

我怎樣才能訪問它? (當然不使用子串方法)

+0

的可能的複製[BeautifulSoup:落後的另一個標籤標籤文本(http://stackoverflow.com/questions/24662053/beautifulsoup-get-tag-text-後面另一個標籤) –

回答

1

這是否適合您?

html = ''' 
     <td valign="top" > 
     <span class="recordAttribute" >Taxonomy</span>: Mollusca, Gastropoda, Littorinimorpha, Hydrobiidae, Hydrobia<br> 
     <span class="recordAttribute" >Identifiers:</span> AF118324[sampleid]    <br> 
     <span class="recordAttribute" >Depository</span>: Mined from GenBank, NCBI     &nbsp; 
     </td> 
     ''' 
soup = BeautifulSoup(html, 'html.parser') 
obj = soup.find('span', text='Identifiers:').nextSibling 
print(obj) 

它打印:

AF118324[sampleid] 
+0

工程就像一個魅力!謝謝 :) – Mornor