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

发布于 2025-02-01 21:48:52 字数 467 浏览 3 评论 0原文

您好,我试图解析文本并提取

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

通常在python中的tiktok URL,以解析URL,我使用以下言论:

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

但是此方法对于tiktok而言失败(我假设它是用于“@@in@in@in” URL

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

我想知道如何使用以下条件或(2)使用另一种方法正确提取我的tiktok

Hello I am trying to parse a text and extract the Tiktok Url that it is in it

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

Normally in python, to parse an url, I use the following regex:

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

But this method fails for tiktok (i assume it is for the "@" in the url)

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

I would like to know how to extract my tiktok urls properly by (1) using a regex or (2) Using another method

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红颜悴 2025-02-08 21:48:52
import re
url = re.search("(?P<url>https?:\/\/[^\s]+)", text).group("url")

您需要逃脱'\'/'

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

You need to escape '\' the '/'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文