當前位置:成語大全網 - 漢語詞典 - Python crawler將在線html頁面中的鏈接替換為本地鏈接,並保存html文件。

Python crawler將在線html頁面中的鏈接替換為本地鏈接,並保存html文件。

導入操作系統,re

def check_flag(標誌):

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

如果regex.match(flag)否則為False,則result = True

回送結果

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

從bs4導入BeautifulSoup

html_content = ' ' '

& lta href = " " & gt測試01

& lta href="/123 " >。測試02

& lta href = " " & gt測試01

& lta href = " " & gt測試01

'''

file = open(r ' favor-en . html ',' r ',encoding="UTF-8 ")

soup = BeautifulSoup(文件,“html.parser”)

對於soup.find_all('img ')中的元素:

如果element.attrs中有“src ”:

print(element.attrs['src'])

if check _ flag(element . attrs[' src ']):

#if element.attrs['src']。查找(" png "):

element . attrs[' src ']= " michenxxxxxxxxxxxx "+'/'+element . attrs[' src ']

打印(" ######################## ")

用open('index.html ',' w ',encoding="UTF-8 ")作為fp:

FP . write(soup . pretify())# pretify()?就是美化sp?,可讀。