urllib.parse.urlparse
urllib.parse.urlparse(urlstring, scheme='', allow_fragments=True)Split a URL string into components. A successful parse is not a promise that the URL is valid, complete, or safe to request.
| Parameter | Description |
|---|---|
urlstring | The text to split. There is no request and no DNS lookup. |
scheme | A default scheme used when the string does not include one. |
allow_fragments | If False, a # fragment is kept as part of the path, parameters, or query instead of a separate field. |
Returns: A ParseResult named tuple. Access fields by name, such as result.scheme and result.netloc, or convert it with tuple().
- This function never raises an error merely because the string does not look like a typical URL.
- The examples stay off the network. Parsing a URL is not the same as fetching it.
from urllib.parse import urlparse
print(urlparse("https://example.com:8443/path?q=1#frag"))
print(urlparse("not a url"))
print(urlparse("javascript:alert(1)"))Expected output
ParseResult(scheme='https', netloc='example.com:8443', path='/path', params='', query='q=1', fragment='frag')
ParseResult(scheme='', netloc='', path='not a url', params='', query='', fragment='')
ParseResult(scheme='javascript', netloc='', path='alert(1)', params='', query='', fragment='')