开发者

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])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜