操纵字符串中的所有url并返回用python修改的新字符串
我需要一个函数来识别其中的所有URL,并对其进行操作,然后用修改后的URL重新创建原始字符串 尝试:操纵字符串中的所有url并返回用python修改的新字符串,python,Python,我需要一个函数来识别其中的所有URL,并对其进行操作,然后用修改后的URL重新创建原始字符串 尝试: old_msg = 'This is an url https://ebay.to/3bxNNfj e this another one https://amzn.to/2QBsX7t' def manipulate_url(url): #example of manipulation, in real i get query replacement tags and other co
old_msg = 'This is an url https://ebay.to/3bxNNfj e this another one https://amzn.to/2QBsX7t'
def manipulate_url(url):
#example of manipulation, in real i get query replacement tags and other complex....
if 'ebay' in url:
new_url = url + "/another/path/"
if 'amzn' in url:
new_url = url + "/lalala/path/"
return new_url
result = re.sub('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', manipulate_url, old_msg)
print(result)
#expected result based on my exmple:
#This is an url https://ebay.to/3bxNNfj/another/path/ e this another one https://amzn.to/2QBsX7t/lalala/path/
但我得到:
TypeError:sequence item 1:expected str instance,re.Match found如文档所述,您提供的函数将收到一个
要获取URL(完全匹配),请在其上使用,如下所示:
重新导入
old_msg='这是一个urlhttps://ebay.to/3bxNNfj 这是另一个吗https://amzn.to/2QBsX7t'
def操作url(匹配):
url=match.group(0)
#操作的例子,在现实中我得到了查询替换标签和其他复杂的。。。。
如果url中有“易趣”:
新建url=url+“/other/path/”
如果url中有“amzn”:
新建url=url+“/lalala/path/”
返回新的url
result=re.sub('http[s]?:/(?:[a-zA-Z]|[0-9]|[$-|&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F])+,操纵url,旧消息)
打印(结果)
输出:
这是一个url,这是另一个
尝试在新的Python3解释器上运行此代码会给出
TypeError:类型为“\u sre.sre\u Match”的参数不可接受
感谢您的解释!它按预期工作。顺便说一句…我的模式识别网址的工作,但…有没有其他更好的或我可以使用这个?谢谢,对我来说,这看起来像是标准的URL正则表达式,我不知道还有什么更好的方法