Issue
I was wondering if it's possible to give a value to a empty node.
Original: <td></td>
Want to change to: <td>anything</td>
Solution
If you find the node using
node = soup.find("td")
then you may provide content to this element by assigning to node.string
:
node.string = "anything"
Full example:
from bs4 import BeautifulSoup
soup = BeautifulSoup('<td></td>')
node = soup.find("td")
node.string = "anything"
print(soup)
Output:
<html><body><td>anything</td></body></html>
If you have many such nodes, and only want to assign to the empty ones, you may do something like:
for node in soup.find_all("td"):
if not node.string:
node.string = "anything"
Answered By - dspencer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.