成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python 如何實(shí)現(xiàn)PHP替換圖片 鏈接

瀏覽:139日期:2022-08-17 10:32:33

問(wèn)題描述

def replace_real_url(html, curr_url): ''' 將 html 中的相對(duì)路徑替換為 絕對(duì)路徑 :param html: :param curr_url: :return: ''' if html and curr_url:pattern = ur’<([a-z]{1,5})[^><]*(href|src)=['’]{0,1}([^'’]+)['’]{0,1}[^><]*>’html = re.sub(pattern, lambda x: replace_real_url_callback(x, curr_url), unicode(html), re.I | re.M) return html def replace_real_url_callback(repl, curr_url): ''' 執(zhí)行替換 :param repl: :param curr_url: :return: ''' ret = repl.group() if repl and repl.lastindex == 3 and repl.group(1).lower() in [’a’, ’img’]:url = urljoin(curr_url, repl.group(3))ret = re.sub(ur’’ + re.escape(repl.group(3)), unicode(url), ret) return ret

如何修正這個(gè)替換的方法。

問(wèn)題解答

回答1:

print re.sub(’(<img src='http://www.piao2010.com/wenda/)(.+?)(' />)’, r’1aa3’, ’aa<img src='http://www.piao2010.com/aaa.jpg' />bb’)# aa<img src='http://www.piao2010.com/wenda/aa' />bb

標(biāo)簽: Python 編程
相關(guān)文章: