import os,re
def check_flag(flag):
regex = re.compile(r'images\/')
result = True if regex.search(flag) else False
return result
#soup = BeautifulSoup(open('index.html'))
from bs4 import BeautifulSoup
html_content = '''
<a href="
https://xxx.com">测试01</a>
<a href="
https://yyy.com/123">测试02</a>
<a href="
https://xxx.com">测试01</a>
<a href="
https://xxx.com">测试01</a>
'''
file = open(r'E:\test\favour-fr.html','r',encoding="UTF-8")
soup = BeautifulSoup(file, 'html.parser')
for element in soup.find_all('img'):
if 'src' in element.attrs:
print(element.attrs['src'])
if check_flag(element.attrs['src']):
#if element.attrs['src'].find("png"):
element.attrs['src'] = "michenxxxxxxxxxxxx" +'/'+ element.attrs['src']
print("##################################")
with open('index.html', 'w',encoding="UTF-8") as fp:
fp.write(soup.prettify()) # prettify()的作⽤是将sp美化⼀下,有可读性