Skip to content Skip to sidebar Skip to footer

How To Replace The IMG Tag Using Python Without Beautifulsoup

I have the following HTML: <

Solution 1:

For the example you've shown - you can probably get away with a regular expression. Note: the correct and robust way is using an HTML parser.

The general idea is to find <img src= that then has something between "s and replace that...

import re
new_html = re.sub('<img src="(.*?)"', '<img src="something else"', old_html)

If this stops working on different pieces of HTML then you'll have to work out another pattern that does work... (case sensitivity, src could appear last in the tag, or there might be more/less spaces, or no quotes, or... etc...)


Post a Comment for "How To Replace The IMG Tag Using Python Without Beautifulsoup"