Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract id from the URL using Python

Tags:

python

I've a URL like this url = 'https://www.example.com/contents/6641345'

I want to extract id at the last of url you can say the interger part from the above string

I tried the solution provided https://stackoverflow.com/a/11339230/2391469 but it gives error you can test that answer as well by removing the starting numeric values and putting some where else in the code, that code will throw an error

can anybody help me to get this id?

like image 677
M K Avatar asked Sep 02 '26 16:09

M K


1 Answers

You can either split the string

>>> url.split('/')[-1]
'6641345'

Or use a regex

>>> import re
>>> re.findall('\d+', url)
['6641345']

Assuming the urls are always of the format that you showed in your example.

like image 134
Cory Kramer Avatar answered Sep 05 '26 07:09

Cory Kramer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!