Web Scrapping using request
Now that you know the basics behind HTTP GET requests, it's time to perform some of your own. In this interactive exercise, you will ping our very own DataCamp servers to perform a GET request to extract information from the first coding exercise of this course, "https://campus.datacamp.com/courses/1606/4135?ex=2"
.
- Import the functions
urlopen
andRequest
from the subpackageurllib.request
. - Package the request to the url
"https://campus.datacamp.com/courses/1606/4135?ex=2"
using the functionRequest()
and assign it torequest
. - Send the request and catch the response in the variable
response
with the functionurlopen()
. - Run the rest of the code to see the datatype of
response
and to close the connection!
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 = Request(url)
# Sends the request and catches the response: response
response = urlopen(request)
html = response.read()
# Print the html
print(html)
# Be polite and close the response!
response.close()
# Print the datatype of response
print(type(response))
Comments
Post a Comment