This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python HTTP requests for scraping, with the exact answer and a short explanation for every question.
The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.
What does the User-Agent header identify?
Answer: The client making the request
Why: A descriptive User-Agent tells the server which client is calling.
Why set a timeout on a request?
Answer: So a stalled connection does not hang forever
Why: A timeout bounds how long the client waits before giving up.
Which status code means the request succeeded?
Answer: 200
Why: 2xx status codes indicate success; 200 is the common OK response.
What is the output?
from urllib.request import Request
request = Request("https://example.com/docs", headers={"User-Agent": "PyPracticeBot/1.0"})
print(request.get_header("User-agent"))Answer: PyPracticeBot/1.0
Why: get_header returns the value stored under the User-Agent header.
Complete the Request import.
from urllib.request import ____Answer: Request
Why: Request builds a request object without sending it.
Complete the keyword that stores request headers.
from urllib.request import Request
request = Request("https://example.com", ____={"User-Agent": "Bot/1.0"})
print(request.get_header("User-agent"))Answer: headers
Why: The headers mapping is passed to the Request constructor.
Complete the upper bound for successful 2xx status codes.
status = 204
print(200 <= status < ____)Answer: 300
Why: Successful responses are 200 through 299, so the exclusive upper bound is 300.
Complete the header name that identifies the client.
from urllib.request import Request
request = Request("https://example.com", headers={"____": "LearningBot/1.0"})
print(request.get_header("User-agent"))Answer: User-Agent
Why: User-Agent is the standard header for client identification.
What does this code print?
from urllib.request import Request
request = Request("https://example.com/a?x=1")
print(request.full_url)Answer: https://example.com/a?x=1
Why: full_url returns the URL exactly as stored in the request.
What does the program output?
from urllib.request import Request
request = Request("https://example.com", headers={"User-Agent": "MyBot/1.0"})
print(request.get_header("User-agent"))Answer: MyBot/1.0
Why: The header lookup returns the value supplied in the headers mapping.
What appears on the screen?
status = 404
print("ok" if 200 <= status < 300 else "error")Answer: error
Why: 404 is outside the 2xx success range, so the conditional prints error.
What is printed?
status = 429
print("retry" if status == 429 else "go")Answer: retry
Why: 429 means too many requests, so the output is retry.
Try the questions first
Open the practice page for Python HTTP requests for scraping and answer the same 12 questions with instant feedback.