Answer key · 12 questions

Python HTTP requests for scraping Exercises With Answers

All 12 questions for Python HTTP requests for scraping in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

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.

01 · Multiple choice

What does the User-Agent header identify?

  1. The page's CSS file
  2. The client making the requestcorrect
  3. The response encoding
  4. The server's database

Answer: The client making the request

Why: A descriptive User-Agent tells the server which client is calling.

02 · Multiple choice

Why set a timeout on a request?

  1. So a stalled connection does not hang forevercorrect
  2. To make the HTML valid
  3. To compress images
  4. To skip robots.txt

Answer: So a stalled connection does not hang forever

Why: A timeout bounds how long the client waits before giving up.

03 · Multiple choice

Which status code means the request succeeded?

  1. 404
  2. 301
  3. 500
  4. 200correct

Answer: 200

Why: 2xx status codes indicate success; 200 is the common OK response.

04 · Multiple choice

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"))
  1. docs
  2. example.com
  3. PyPracticeBot/1.0correct
  4. None

Answer: PyPracticeBot/1.0

Why: get_header returns the value stored under the User-Agent header.

05 · Fill in the blank

Complete the Request import.

from urllib.request import ____

Answer: Request

Why: Request builds a request object without sending it.

06 · Fill in the blank

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.

07 · Fill in the blank

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.

08 · Fill in the blank

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.

09 · Predict output

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.

10 · Predict output

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.

11 · Predict output

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.

12 · Predict output

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.

Practice this lesson