2

Hi how to use python to transform the url of a article to it's print url.

article url:http://www.indianexpress.com/news/second-time-as-farce/800228/0

print url:http://www.indianexpress.com/story-print/800228/

How to convert article url to print url?

2 Answers 2

5

Use urllib.parse.urlparse() to carve the path from the rest of the url, and posixpath.split() and posixpath.join() to reform the path, and urllib.parse.urlunparse() to put it all back together again.

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

4 Comments

Out of curiosity, is posixpath necessary? urlparse is always going to return a forward slash.
You don't want to use os.path, since on Windows that will give you ntpath instead.
Is there something wrong with hard-coding a forward slash in this case?
Not per se, but there's no need to; "magic numbers" aren't restricted to numbers.
0
from urllib.parse import urlparse

def transform(url):
    parsed = urlparse(url)
    return '{0}://{1}/story-print/{2}/'.format(parsed.scheme, parsed.netloc, parsed.path.split('/')[-2])

1 Comment

Always be sure to check the tags.

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.