Created
November 5, 2015 07:05
-
-
Save peketamin/3631a5cc3f648ce26393 to your computer and use it in GitHub Desktop.
相対URL(relative_url)を絶対URL(base_url)の末尾ファイル名部分と置換してURLを組み立てる
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def build_absurl_with_relurl(base_url, relative_url): | |
""" | |
相対URL(relative_url)を絶対URL(base_url)の末尾ファイル名部分と | |
置換してURLを組み立てる | |
http://www.example.jp/main/sec/2015/20151027001.html | |
+ | |
images/20151027001a.jpg | |
= | |
http://www.example.jp/main/sec/2015/images/20151027001a.jpg | |
""" | |
o = urlparse(base_url) | |
_basename = basename(o.path) | |
new_relative_path = o.path.replace(_basename, relative_url) | |
url = urlunparse(( | |
o.scheme, | |
o.netloc, | |
new_relative_path, | |
o.params, | |
o.query, | |
o.fragment, | |
)) | |
return url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment