0

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?

2
  • 3
    you can use rsplit() like this s.rstrip('/').rsplit('/', 1)[-1] Commented Jul 27, 2021 at 7:40
  • how about ID = (link.lstrip("https://www.linkedin.com/in/").rsplit('/'))[0] Commented Jul 27, 2021 at 7:44

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

add some explanation don't post code only answers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.