python 如何實現PHP替換圖片 鏈接
問題描述
def replace_real_url(html, curr_url): ''' 將 html 中的相對路徑替換為 絕對路徑 :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): ''' 執行替換 :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
如何修正這個替換的方法。
問題解答
回答1:print re.sub(’(<img src='http://www.baoyu77737.com/wenda/)(.+?)(' />)’, r’1aa3’, ’aa<img src='http://www.baoyu77737.com/aaa.jpg' />bb’)# aa<img src='http://www.baoyu77737.com/wenda/aa' />bb
相關文章:
1. Java中call by value和call by reference的區別2. javascript - history.replaceState()無法改變query參數3. css - 關于偽類背景問題4. node.js - 在vuejs-templates/webpack中dev-server.js里為什么要exports readyPromise?5. html - 移動端radio無法選中6. CSS3 border凹陷該如何編寫?7. html5 - 如何實現圖中的刻度漸變效果?8. javascript - 關于禁用文本選擇與復制的問題9. 前端 - html5 audio不能播放10. css3 - transition屬性當鼠標一開的時候設置的時間不起作用
