2014-12-22 119 views

回答

4

找到並加入所有text nodesp標記,並檢查它的父不是一個a標籤:

p = soup.find("td", {"id": "overview-top"}).find("p", {"itemprop": "description"}) 

print ''.join(text for text in p.find_all(text=True) 
       if text.parent.name != "a") 

演示(見無link text印刷):

>>> from bs4 import BeautifulSoup 
>>> 
>>> data = """ 
... <td id="overview-top"> 
...  <p itemprop="description"> 
...   text1 
...   <a href="google.com">link text</a> 
...   text2 
...  </p> 
... </td> 
... """ 
>>> soup = BeautifulSoup(data) 
>>> p = soup.find("td", {"id": "overview-top"}).find("p", {"itemprop": "description"}) 
>>> print p.text 

     text1 
     link text 
     text2 
>>> 
>>> print ''.join(text for text in p.find_all(text=True) if text.parent.name != "a") 

     text1 

     text2 
1

使用LXML,

import lxml.html as LH 

data = """ 
<td id="overview-top"> 
    <p itemprop="description"> 
     text1 
     <a href="google.com">link text</a> 
     text2 
    </p> 
</td> 
""" 

root = LH.fromstring(data) 
print(''.join(root.xpath(
    '//td[@id="overview-top"]//p[@itemprop="description"]/text()'))) 

收益率

 text1 

     text2 

也得到的<p>子標籤的文本,只使用一個雙斜槓,//text(),而不是一個單一的正斜槓:

print(''.join(root.xpath(
    '//td[@id="overview-top"]//p[@itemprop="description"]//text()'))) 

產量

 text1 
     link text 
     text2 
相關問題