2016-01-08 105 views
1

任何人都有一個想法,您將如何使用美麗的湯刪除td元素之間的空格?使用BeautifulSoup刪除td元素之間的空格

例如

<table> 
    <tr class="soup-target"> 
    <td></td>  <td></td> 
    </tr> 
</table> 

你之前說「只使用刪除鍵」,不可能的,因爲我使用的模板語言,在td元素循環,和語言不允許控制環形元素上的空間或換行符的空間。

回答

1

您還可以過濾tr直接在文本節點,並提取它們:

row = soup.find("tr", class_="soup-target") 
for text_node in row.find_all(text=True, recursive=False): 
    text_node.extract() 
0

想通這一個

el = soup.find_all('tr', {'class': 'soup-target'}) 
if el: 
    for node in el: 
     for child in node.children: 
      if isinstance(child, NavigableString): 
       child.extract()