urllib
- Python course
- Advanced
- Lesson 37/83
- 5 min
Learn basic HTTP requests with urllib.request.
urllib
urllib.request is Python's standard-library module for making HTTP requests. The urlopen function opens a URL and returns a response object.
import urllib.request
resp = urllib.request.urlopen("data:text/plain,merhaba")
print(resp.read().decode()) # merhabaURLs of the form data:text/plain,... return local data; no network access is needed. resp.read() gives the body as bytes; .decode() turns it into a string.
read and decode
The response object's read() method returns the body as bytes. To turn the byte string into a human-readable string, call .decode().
resp = urllib.request.urlopen("data:text/plain,merhaba")
body = resp.read() # b'merhaba'
text = body.decode() # 'merhaba'Spaces and Turkish characters decode correctly because UTF-8 is the default encoding.
Practice Read local data with urllib
Start with import urllib.request. Call urllib.request.urlopen("data:text/plain,merhaba dünya") to get a response. Decode the body with resp.read().decode() and print it.
Starter code
# import the urllib.request module
# open the data: URL with urlopen
# decode the body with read().decode() and print itSample solution
import urllib.request
resp = urllib.request.urlopen("data:text/plain,merhaba dünya")
print(resp.read().decode())Expected output
merhaba dünya
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What kind of object does urllib.request.urlopen return?
- A list
- An HTML string
- A dict
- A response object Correct answer
Explanation: urlopen returns a response object; read() reads the body.
2 Complete the urlopen call
import urllib.request
resp = urllib.request.____("data:text/plain,merhaba")
import urllib.request
resp = urllib.request.urlopen("data:text/plain,merhaba")
Explanation: urllib.request.urlopen(url) opens the URL.
3 Match the urllib concepts
| Term | Meaning |
|---|---|
urlopen |
Opens a URL |
read |
Reads body as bytes |
decode |
Turns into string |
data: URL |
Gives local data |