开发者

Slicing url with Python

开发者 https://www.devze.com 2023-03-13 13:25 出处:网络
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

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

精彩评论

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

关注公众号