Slicing url with Python
Hi how to use python to transform the url of a ar开发者_开发技巧ticle 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?
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.
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])
精彩评论