Issue
I'm trying to make a program that looks through the website you're on, and censors bad words. I can use CTRL-shift-c to pull up Chrome Dev tools, and go through each element and erase the words I don't want easily enough, but how might I do that with code?
I understand a little about web-scrapping using beautifulsoup, so finding the no no words should be simple enough, but I have no idea how to give that program the ability to edit the HTML of the website.
I'm thinking there are two solutions; I can scrape each element for its text, and then re-insert the new text in its place, or I would have to snatch a copy of the website's HTML, have my code scan and edit it, and then upload this new HTML in its place?
The more I think about it the less feasible it seems.
Thanks for your help.
Solution
I think I know what you might want, something akin to find and replace, I tried to sketch up something quick in Python:
from bs4 import BeautifulSoup
def find_and_replace(html_content, keyword, replacement):
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(html_content, "html.parser")
# Find all elements containing the keyword
elements_with_keyword = soup.find_all(lambda tag: keyword.lower() in tag.get_text().lower() if tag.name else False)
# Replace the content of each found element
for element in elements_with_keyword:
element.string = replacement
return str(soup)
# Read the HTML file
file_path = "example.html"
with open(file_path, "r", encoding="utf-8") as file:
html_content = file.read()
# Specify the keyword you want to find and the replacement text
keyword_to_find = "example"
replacement_text = "poop"
# Find and replace the keyword in the HTML content
modified_html_content = find_and_replace(html_content, keyword_to_find, replacement_text)
# Write the modified content back to the file
with open(file_path, "w", encoding="utf-8") as file:
file.write(modified_html_content)
print(f'Keyword "{keyword_to_find}" replaced with "{replacement_text}" in the file "{file_path}".')
Of course, if you have multiple words, you might want to use a for loop and a list for that, but that is up to you.
I think I forgot to mention this is local HTML only, if you want something for the web ( client side ) I suggest dabbling into extensions ( as in they will only be censored by the one using the extension or something like that ) well, I think I got downvoted because I just gave you some code but, here is some example of what you could achieve: chromewebstore.google.com/detail/censor-me/… read this for more info : developer.chrome.com/docs/extensions/get-started/tutorial/… using plain old javascript should be enough.
Answered By - Alex Aly
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.