I have the HTML code of a website and I am trying to modify it to change all of the website links with the ones in my home directory. The HTML is stored in a variable called soup. Here are some of the things that I tried:
Example:
website link: http://Url.com/image1
modified link: file://local.html/image1
def replace_all(text, dic):
for i, j in dic.items():
text = text.replace(i, j)
return text
od = OrderedDict(["http://Url.com", "local"])
replace_all(soup, od)
But I get this error:
ValueError: too many values to unpack (expected 2)
Do you guys know how to make it happen?
CodePudding user response:
The error is happening because the OrderedDict constructor is not receiving the correct arguments. Regardless though, it seems that you don't need a dictionary structure, because the replace
method takes care of the replacement for you. So you can just do this:
def replace_all(text, to_replace):
for old, new in to_replace:
text = text.replace(old, new)
return text
to_replace = [("http://Url.com", "local"), ("url2_old", "url2_new"), ]
replace_all(soup, to_replace)
CodePudding user response:
An alternative is to operate on a string of the whole HTML source using regex.
import re
reg = re.compile(r'http://url.com')
s = """
<a href="http://url.com"></a>
<img src="http://url.com/goodpic.jpg"/>
"""
print(reg.sub('local', s))