Relative url handling in python

There is a very useful class called os.path in python, but when combining absolute and relative paths,

# abs_path = "/usr/hoge/", rel_path = "./poyo/"
path.normpath(path.join(abs_path, rel_path))
# "/usr/hoge/poyo"

I often do it. I thought this could be used with url,

# abs_path = "http://hoge/", rel_path = "./poyo/"
path.normpath(path.join(abs_url, rel_url) 
# "http:/hoge/poyo/"

I was angry that I couldn't open the url. That's why I was a little worried, but the reason was simple: the url "http: // hoge /" was converted to "http: / hoge" in path.normpath. Since path is a class for file system paths, it had this specification. It seems to use the urljoin method of the urlparse class to join urls.

# abs_path = "http://hoge/", rel_path = "./poyo/"
urlparse.urljoin(abs_url, rel_url)
# "http://hoge/poyo/"

Recommended Posts

Relative url handling in python
Handling json in python
Handling of JSON files in Python
Handling timezones in Python (datetime, pytz)
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
Check if the URL exists in Python
quicksort in python
nCr in python
Python Error Handling
Python exception handling
Plink in Python
Constant in python
Download images from URL list in Python
Lifegame in Python.
FizzBuzz in Python
Sample for handling eml files in Python
Sqlite in python
StepAIC in Python
Python timezone handling
N-gram in python
LINE-Bot [0] in Python
Disassemble in Python
Reflection in Python
Constant in python
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Module import and exception handling in python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
Convert absolute URLs to relative URLs in Python
LiNGAM in Python
Python exception handling
Flatten in python
flatten in python
Get image URL using Flickr API in Python
Issue the Amazon CloudFront Signed URL in Python
Put out a shortened URL string in Python
Sorted list in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python