How can I remove a NavigableString from the tree?

Dany

I am a bit confused: all tags have a decompose() method which allows to remove the tag from the tree in place. But what if I want to remove a NavigableString? It doesn't have such method:

>>> b = BeautifulSoup('<p>aaaa <span> bbbbb </span> ccccc</p>', 'html.parser')
>>> b.p.contents[0]
'aaaa '
>>> type(b.p.contents[0])
<class 'bs4.element.NavigableString'>
>>> b.p.contents[0].decompose()
Traceback (most recent call last):
...
AttributeError: 'NavigableString' object has no attribute 'decompose'

There's a way I managed to somewhat remove the NavigableString from the tree: by removing it from the content list:

>>> b.p.contents.pop(0)
'aaaa '
>>> b
<p><span> bbbbb </span> ccccc</p>

The problem is that it is still present in the strings method response:

>>> list(b.strings)
['aaaa ', ' bbbbb ', ' ccccc']

Which shows that it was wrong way to do. Besides, I am using strings in my code so this hacky solution is not acceptable, alas.


So the question is: how can I remove the specific NavigableString object from the tree?

KunduK

Use extract() instead of decompose()

extract() removes a tag or string from the tree.

decompose() removes a tag from the tree.

b = BeautifulSoup('<p>aaaa <span> bbbbb </span> ccccc</p>', 'html.parser')
b.p.contents[0].extract()
print(b)

To Know more about it please check following link where you will find more details. BeautifulSoup

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How can I remove an entry from a list in a shells script?

分類Dev

How can I remove the last 4 characters from a string?

分類Dev

How can I remove duplicate objects with distinctBy from a list in Kotlin?

分類Dev

How can i remove extra "s" from django admin panel?

分類Dev

How can I remove tralling backslash from $(ProjectDir)?

分類Dev

How can I remove object from array, with Lodash?

分類Dev

How can I remove words from an array that are after a new line?

分類Dev

How can i remove and rename items from "Sort by" in prestashop?

分類Dev

how can i remove an extra line from my plot?

分類Dev

In swift, how can i remove an object from my tableview?

分類Dev

How can I remove all but the last N elements from an array?

分類Dev

How can i remove default space occupied by label from group?

分類Dev

how can i add and remove element dynamically from linear layout?

分類Dev

How can I remove certain string from file name?

分類Dev

How can I remove delta-homes.com from browsers?

分類Dev

How can I remove every other item from a Sass list?

分類Dev

How can I remove zero values from list?

分類Dev

How do I avoid the 'NavigableString' error with BeautifulSoup and get to the text of href?

分類Dev

how to extract string or number from bs4.element.NavigableString

分類Dev

How can I plot a tree (and squirrels) in R?

分類Dev

How can I improve the accuracy of my prediction from a decision tree model using sklearn?

分類Dev

How can i replace icons from tree.store with glyphs in Extjs 4.2?

分類Dev

If I build a package from source how can I uninstall or remove completely?

分類Dev

How can I remove some keys from a JSON string where the values are preventing me from parsing it as JSON?

分類Dev

How can I remove pins from google maps when selecting new location from select menu?

分類Dev

In pandas DataFrame, how can I store a specific value from a column into a variable, and then subsequently remove that value from the column?

分類Dev

How can I remove a list from another list using a field value of the element's type if i can't modify the class directly?

分類Dev

How can i remove a particular comma at end

分類Dev

How can I remove an applied git patch?

Related 関連記事

  1. 1

    How can I remove an entry from a list in a shells script?

  2. 2

    How can I remove the last 4 characters from a string?

  3. 3

    How can I remove duplicate objects with distinctBy from a list in Kotlin?

  4. 4

    How can i remove extra "s" from django admin panel?

  5. 5

    How can I remove tralling backslash from $(ProjectDir)?

  6. 6

    How can I remove object from array, with Lodash?

  7. 7

    How can I remove words from an array that are after a new line?

  8. 8

    How can i remove and rename items from "Sort by" in prestashop?

  9. 9

    how can i remove an extra line from my plot?

  10. 10

    In swift, how can i remove an object from my tableview?

  11. 11

    How can I remove all but the last N elements from an array?

  12. 12

    How can i remove default space occupied by label from group?

  13. 13

    how can i add and remove element dynamically from linear layout?

  14. 14

    How can I remove certain string from file name?

  15. 15

    How can I remove delta-homes.com from browsers?

  16. 16

    How can I remove every other item from a Sass list?

  17. 17

    How can I remove zero values from list?

  18. 18

    How do I avoid the 'NavigableString' error with BeautifulSoup and get to the text of href?

  19. 19

    how to extract string or number from bs4.element.NavigableString

  20. 20

    How can I plot a tree (and squirrels) in R?

  21. 21

    How can I improve the accuracy of my prediction from a decision tree model using sklearn?

  22. 22

    How can i replace icons from tree.store with glyphs in Extjs 4.2?

  23. 23

    If I build a package from source how can I uninstall or remove completely?

  24. 24

    How can I remove some keys from a JSON string where the values are preventing me from parsing it as JSON?

  25. 25

    How can I remove pins from google maps when selecting new location from select menu?

  26. 26

    In pandas DataFrame, how can I store a specific value from a column into a variable, and then subsequently remove that value from the column?

  27. 27

    How can I remove a list from another list using a field value of the element's type if i can't modify the class directly?

  28. 28

    How can i remove a particular comma at end

  29. 29

    How can I remove an applied git patch?

ホットタグ

アーカイブ