Python:如何从给定文本中解析Tiktok url



你好,我正在尝试解析文本并提取它在其中的抖音Url

text = "This is a text https://www.tiktok.com/@tt_user/video/7620173214578963251 and more text"

通常在python中,为了解析url,我使用以下正则表达式:

import re
url = re.search("(?P<url>https?://[^s]+)", text).group("url")

但是这个方法对于tiktok失败了(我假设它是对于"@"在url中)

AttributeError: 'NoneType' object has no attribute 'group'

我想知道如何通过(1)使用正则表达式或(2)使用另一种方法正确地提取我的抖音url

import re
url = re.search("(?P<url>https?://[^s]+)", text).group("url")

你需要转义'' '/'

最新更新