python 如何實(shí)現(xiàn)PHP替換圖片 鏈接
問(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://m.lshqa.cn/wenda/)(.+?)(' />)’, r’1aa3’, ’aa<img src='http://m.lshqa.cn/aaa.jpg' />bb’)# aa<img src='http://m.lshqa.cn/wenda/aa' />bb
相關(guān)文章:
1. Docker for Mac 創(chuàng)建的dnsmasq容器連不上/不工作的問(wèn)題2. html5 - javascript讀取自定義屬性的值,有的能夠取到,有的取不到怎么回事??3. html - Python2 BeautifulSoup 提取網(wǎng)頁(yè)中的表格數(shù)據(jù)及連接4. python - PyCharm里的一個(gè)文件不小心忽略了wx包5. android - VideoView與百度Map沖突6. python - (2006, ’MySQL server has gone away’)7. 小白學(xué)python的問(wèn)題 關(guān)于%d和%s的區(qū)別8. python - 使用eclipse運(yùn)行django代碼,修改了views.py這個(gè)文件,但是瀏覽器顯示的還是原有沒(méi)修改的結(jié)果,怎么處理?9. django - pycharm 如何配置 python3 的開(kāi)發(fā)環(huán)境?10. win10 Apache24+PHP8.0,Apache不能正常加載php.ini。
