41

Is it possible to build a URL using the Requests library for Python?

Building a query string is supported but what about building the rest of the URL. Specifically I'd be interested in adding on to the base URL with URL encoded strings:

http :// some address.com/api/[term]/

term = 'This is a test'

http :// some address.com/api/This+is+a+test/

This is presumably possible using urllib but it seems like it would be better in Requests. Does this feature exist? If not is there a good reason that it shouldn't?

1 Answer 1

63

requests is basically a convenient wrapper around urllib (and 2,3 and other related libraries).

You can import urljoin(), quote() from requests.compat, but this is essentially the same as using them directly from urllib and urlparse modules:

>>> from requests.compat import urljoin, quote_plus
>>> url = "http://some-address.com/api/"
>>> term = 'This is a test'
>>> urljoin(url, quote_plus(term))
'http://some-address.com/api/This+is+a+test'
Sign up to request clarification or add additional context in comments.

3 Comments

I like requests.compat.urljoin because it works in both Python 2 and 3.
@Addy - seems to work well for me. Can you be more specific in which scenarios it doesn't work?
can't find urljoin from requests.compat?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.