Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

Python: confusions with urljoin

I am trying to form URLs from different pieces, and having trouble understanding the behavior of this method. For example:

Python 3.x

from urllib.parse import urljoin

>>> urljoin('some', 'thing')
'thing'
>>> urljoin('http://some', 'thing')
'http://some/thing'
>>> urljoin('http://some/more', 'thing')
'http://some/thing'
>>> urljoin('http://some/more/', 'thing') # just a tad / after 'more'
'http://some/more/thing'
urljoin('http://some/more/', '/thing')
'http://some/thing'

Can you explain the exact behavior of this method?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The best way (for me) to think of this is the first argument, base is like the page you are on in your browser. The second argument url is the href of an anchor on that page. The result is the final url to which you will be directed should you click.

>>> urljoin('some', 'thing')
'thing'

This one makes sense given my description. Though one would hope base includes a scheme and domain.

>>> urljoin('http://some', 'thing')
'http://some/thing'

If you are on a vhost some, and there is an anchor like <a href='thing'>Foo</a> then the link will take you to http://some/thing

>>> urljoin('http://some/more', 'thing')
'http://some/thing'

We are on some/more here, so a relative link of thing will take us to /some/thing

>>> urljoin('http://some/more/', 'thing') # just a tad / after 'more'
'http://some/more/thing'

Here, we aren't on some/more, we are on some/more/ which is different. Now, our relative link will take us to some/more/thing

>>> urljoin('http://some/more/', '/thing')
'http://some/thing'

And lastly. If on some/more/ and the href is to /thing, you will be linked to some/thing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...