Aytida
Aytida

Reputation: 189

Unable to get data from string in python

I am trying to get the 'id' of LinkedIn profiles using Python. By ID, I mean from https://www.linkedin.com/in/adigup21/, it should get adigup21. I am using this trick ID = (link.lstrip("https://www.linkedin.com/in/").rstrip('/')) But for some cases, it misses out on characters or is blank (I always make sure the format is same and good)

Is there any accurate alternative present for this?

Upvotes: 0

Views: 49

Answers (1)

Alien Life Form
Alien Life Form

Reputation: 1944

link.rstrip('/').split('/').pop()

rstrip removes the (optional) final slash, split makes an array out of the slash-separated parts, pop extracts the last element.

BTW, this is just a hack. Manipulating URLs elements is best done with URL parsing, along the lines of

pth=urllib.parse.urlparse(link).path

One can then do rstrip/split/pop thing on pth.

Upvotes: 2

Related Questions