Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Python Dictionary

  1. Home
  2. Python Dictionary
  3. urllib.request.urlopen()

urllib.request.urlopen()

urllib is an HTTP client included in Python's standard library that lets you fetch web pages and call APIs without any external packages. Use urllib.request.urlopen() to access a URL and urllib.parse.urlencode() to build query strings. In production, the requests library is easier to work with, but there are situations where you must rely on the standard library alone.

Syntax

from urllib import request, parse

# GET request
with request.urlopen(url) as res:
    data = res.read()

# POST request
data = parse.urlencode({'key': 'value'}).encode()
with request.urlopen(url, data=data) as res:
    result = res.read()

# URL encoding
encoded = parse.urlencode({'q': 'search keyword'})
quoted  = parse.quote('hello world')

Functions and Classes

Function / ClassDescription
request.urlopen(url)Sends a GET request to the URL and returns a response object.
request.urlopen(url, data)Providing data sends a POST request instead.
request.Request(url, headers)Creates a request object with custom headers and other options.
res.read()Reads the response body as bytes.
res.statusReturns the HTTP status code (Python 3.9+).
res.getheader(name)Returns the value of the specified response header.
parse.urlencode(dict)Converts a dictionary to a query string (key=value&key2=value2).
parse.quote(string)URL-encodes a string (spaces become %20).
parse.quote_plus(string)URL-encodes a string (spaces become +).
parse.unquote(string)Decodes a URL-encoded string.
parse.urlparse(url)Breaks a URL into its components: scheme, host, path, and more.

Sample Code

from urllib import request, parse
import json

# GET request
url = 'https://jsonplaceholder.typicode.com/todos/1'
try:
    with request.urlopen(url, timeout=10) as res:
        data   = res.read()
        status = res.status
        ctype  = res.getheader('Content-Type')
        print(f"Status: {status}")               # 200
        print(f"Content-Type: {ctype}")
        result = json.loads(data.decode('utf-8'))
        print(result)                             # {'userId': 1, 'id': 1, ...}
except Exception as e:
    print(f"Error: {e}")

# GET request with query string
params = {'q': 'Python', 'page': 1, 'limit': 10}
query  = parse.urlencode(params)
url    = f'https://api.example.com/search?{query}'
print(url)  # https://api.example.com/search?q=Python&page=1&limit=10

# Request with custom headers
req = request.Request(
    'https://api.example.com/data',
    headers={
        'User-Agent': 'MyApp/1.0',
        'Accept':     'application/json',
    }
)
# with request.urlopen(req) as res: ...

# POST request (application/x-www-form-urlencoded)
post_data = parse.urlencode({'name': 'Alice', 'age': 30}).encode('utf-8')
req = request.Request(
    'https://httpbin.org/post',
    data=post_data,
    headers={'Content-Type': 'application/x-www-form-urlencoded'},
)
# with request.urlopen(req) as res: ...

# URL encoding and decoding
text    = 'hello world'
encoded = parse.quote(text)
print(encoded)              # hello%20world
decoded = parse.unquote(encoded)
print(decoded)              # hello world

# Parsing a URL
parsed = parse.urlparse('https://example.com:8080/path?key=val#section')
print(parsed.scheme)        # https
print(parsed.netloc)        # example.com:8080
print(parsed.path)          # /path
print(parsed.query)         # key=val
print(parsed.fragment)      # section

Notes

urllib.request.urlopen() supports HTTPS and validates SSL certificates by default. You can set a timeout in seconds using the timeout parameter — always set one, because omitting it causes the program to wait indefinitely for a server response.

The response body is returned as bytes, so decode it with the appropriate encoding (usually UTF-8) before treating it as a string. You can also retrieve the encoding from the Content-Type response header.

If you make frequent HTTP requests in production, the requests library is recommended. It makes session management, authentication, and retries straightforward, and results in more concise code. When you can install external packages, run pip install requests to get started.

If you find any errors or copyright issues, please .