成语大全网 - 汉语词典 - python爬虫 将在线html网页中的链接替换成本地链接并将html文件到本地

python爬虫 将在线html网页中的链接替换成本地链接并将html文件到本地

import os,re

def check_flag(flag):

regex = re.compile(r'images\/')

result = True if regex.match(flag) else False

return result

#soup = BeautifulSoup(open('index.html'))

from bs4 import BeautifulSoup

html_content = '''

<a href="">测试01</a>

<a href="/123">测试02</a>

<a href="">测试01</a>

<a href="">测试01</a>

'''

file = open(r'favour-en.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美化?下,有可读性