Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build a URL using Requests module Python

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?

like image 577
Jimbo Avatar asked May 16 '14 02:05

Jimbo


People also ask

What is url in request in Python?

url returns the URL of the response. It will show the main url which has returned the content, after all redirections, if done. Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object.

How would you request a webpage using Python import requests?

Python requests reading a web pageThe get method issues a GET request; it fetches documents identified by the given URL. The script grabs the content of the www.webcode.me web page. The get method returns a response object. The text attribute contains the content of the response, in Unicode.


1 Answers

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' 
like image 81
alecxe Avatar answered Sep 17 '22 20:09

alecxe