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.lshqa.cn/wenda/)(.+?)(' />)’, r’1aa3’, ’aa<img src='http://www.lshqa.cn/aaa.jpg' />bb’)# aa<img src='http://www.lshqa.cn/wenda/aa' />bb
相關文章:
1. javascript - node.js promise沒用2. android 如何實現如圖中的鍵盤上的公式及edittext的內容展示呢3. c++ - 如何正確的使用QWebEngineView?4. golang - 用IDE看docker源碼時的小問題5. javascript - js 寫一個正則 提取文本中的數據6. 算法 - python 給定一個正整數a和一個包含任意個正整數的 列表 b,求所有<=a 的加法組合7. yii2中restful配置好后在nginx下報404錯誤8. java - 我在用Struts2上傳文件時,報以下錯誤怎么回事?9. PHP注冊功能10. php - 注冊驗證郵箱失效后操作問題
