Issue
Let's presume I have the following piece of code
text = '<link href="link">'
souped = BeautifulSoup(text, 'html.parser')
tags = souped.find_all()
link_modified_tags = ResultSet()
for tag in tags:
if tag.has_attr('href')
link_modified_tag = tag
link_modified_tag['href'] = 'modified_link'
link_modified_tags.append(tag_copy)
else:
link_modified_tags.append(tag)
link_modified_souped = souped
How would I put the modified ResultSet back into link_modified_souped
for further to-text transformation?
Solution
You don't put back modified tags, you modify tags in-place:
from bs4 import BeautifulSoup
text = '<link href="link">'
souped = BeautifulSoup(text, "html.parser")
tags = souped.find_all()
for tag in tags:
if tag.has_attr("href"):
tag["href"] = "modified_link"
print(souped)
Prints:
<link href="modified_link"/>
Answered By - Andrej Kesely
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.