Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python: How to parse a Tiktok url from a given text

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

You need to escape ‘\’ the ‘/’

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading