Get Started

Printing HTTP request results in Python using urllib

You have just packaged and sent a GET request to "https://campus.datacamp.com/courses/1606/4135?ex=2" and then caught the response. You saw that such a response is a http.client.HTTPResponse object. The question remains: what can you do with this response?

Well, as it came from an HTML page, you could read it to extract the HTML and, in fact, such a http.client.HTTPResponse object has an associated read() method. In this exercise, you'll build on your previous great work to extract the response and print the HTML.

This is a part of the course

“Intermediate Importing Data in Python”

View Course

Exercise instructions

  • Send the request and catch the response in the variable response with the function urlopen(), as in the previous exercise.
  • Extract the response using the read() method and store the result in the variable html.
  • Print the string html.
  • Hit submit to perform all of the above and to close the response: be tidy!

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Import packages
from urllib.request import urlopen, Request

# Specify the url
url = "https://campus.datacamp.com/courses/1606/4135?ex=2"

# This packages the request
request = Request(url)

# Sends the request and catches the response: response


# Extract the response: html


# Print the html


# Be polite and close the response!
response.close()

This exercise is part of the course

Intermediate Importing Data in Python

IntermediateSkill Level
4.6+
35 reviews

Improve your Python data importing skills and learn to work with web and API data.

The web is a rich source of data from which you can extract various types of insights and findings. In this chapter, you will learn how to get data from the web, whether it is stored in files or in HTML. You'll also learn the basics of scraping and parsing web data.

Exercise 1: Importing flat files from the webExercise 2: Importing flat files from the web: your turn!Exercise 3: Opening and reading flat files from the webExercise 4: Importing non-flat files from the webExercise 5: HTTP requests to import files from the webExercise 6: Performing HTTP requests in Python using urllibExercise 7: Printing HTTP request results in Python using urllib
Exercise 8: Performing HTTP requests in Python using requestsExercise 9: Scraping the web in PythonExercise 10: Parsing HTML with BeautifulSoupExercise 11: Turning a webpage into data using BeautifulSoup: getting the textExercise 12: Turning a webpage into data using BeautifulSoup: getting the hyperlinks

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free