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='https://www.haobala.com/wenda/)(.+?)(' />)’, r’1aa3’, ’aa<img src='https://www.haobala.com/aaa.jpg' />bb’)# aa<img src='https://www.haobala.com/wenda/aa' />bb
相关文章:
1. javascript - 百度坐标,返回目标附近有哪些坐标2. javascript - 关于jsp中嵌入js代码的问题3. html - 急求,能否用flex或者高端点的CSS来布局这个图呢?不想用浮动或定位了4. PHP类中的$this5. angular.js - angularjs 百度统计怎么统计6. angular.js - angurlarJS的安卓的微信端中用加载ngCookies会出错的问题7. javascript - 关于fullpage.js的横向滚动控制8. python 正则表达式提取9. javascript - 有什么兼容性比较好的办法来判断浏览器窗口的类型?10. Python操作sqlserver是异步的吗?

网公网安备