扫码关注官方订阅号
ringa_lee
不知道为什么直接引用匹配组作为urljoin的参数不行,但是在匿名函数内调用urljoin并将匹配组传入urljoin是可以的,如下:
import re from urlparse import urljoin pageUrl = 'http://exapmple.com/a/b/c.htm' itemContent = '<p>\n<a HrEf="/slash.html">slash</a>\n<a HrEf="1.html">page</a>\n<a hrEF="./2.html">1dot</a>\n<a hrEF="../3.html">2dots</a>\n<a hrEF="www.baidu.com/2.html">host</a>\n<a hrEF="http://www.baidu.com/index.html">fullurl</a>\n</p>' p = re.compile('href=[\'"](?P<url>(?!(https?://)).*?)[\'"]', re.I) print p.sub(lambda x: 'href="' + urljoin(pageUrl, x.group(1)) + '"', itemContent)
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
扫描下载App
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
不知道为什么直接引用匹配组作为urljoin的参数不行,但是在匿名函数内调用urljoin并将匹配组传入urljoin是可以的,如下: