Python get the HTML content

get content

Table of contents:

Get html content using urllib

You will probable ignore to use urllib to get HTTP content from the web page, since there is newer module called requests for that. However, if you need to use urllib, here is the tip:


import urllib.request
url = ""
r = urllib.request.urlopen(url)
b =


b'<!DOCTYPE html>\n<html lang="en">\n<head>\n<meta charset="utf-8"> ... '

urllib, urllib2 and urllib3 story: urllib2 is not present anymore, it has been split to urllib.request and urllib.error. urllib you may forget, since there is newer urllib3, part of the requests. In other words, just use requests.

Get html content using requests

First you need to install requests from the command line:

pip install requests

Then you chan check:

pip show requests


Summary: Python HTTP for Humans.
Requires: urllib3, chardet, certifi, idna

These are requests module requirements, telling you it uses latest urllib3.

package description
urllib3 HTTP library with thread-safe connection pooling, file post, and more.
chardet Universal encoding detector for Python 3.
certfify Package for providing Mozilla’s CA Bundle.
idna Internationalized Domain Names in Applications (IDNA)


import requests
url = ""
    r = requests.get(url) # requests.models.Response
    d = ['apparent_encoding', 'close', 'connection', 'content', 'cookies', 'elapsed', 'encoding',
     'headers', 'history', 'is_permanent_redirect', 'is_redirect', 'iter_content', 'iter_lines',
     'json', 'links', 'next', 'ok', 'raise_for_status', 'raw', 'reason', 'request', 'status_code',
     'text', 'url']
    for p in d:
        print(getattr(r, p))
    print("Error with the request")


<bound method Response.close of <Response [200]>>
<requests.adapters.HTTPAdapter object at 0x0000013AC844D888>
b'<!DOCTYPE html>\n<html lang="en">...</html>\n'
<RequestsCookieJar[<Cookie __cfduid=d97d6be96c548c62b6e006d99fa31933f1580069304 for>]>
{'Date': 'Sun, 26 Jan 2020 20:08:24 GMT', 'Content-Type': 'text/html; charset=utf-8', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', ... , 'Content-Encoding': 'gzip'}
<bound method Response.iter_content of <Response [200]>>
<bound method Response.iter_lines of <Response [200]>>
<bound method Response.json of <Response [200]>>
<bound method Response.raise_for_status of <Response [200]>>
<urllib3.response.HTTPResponse object at 0x0000013AC845CDC8>
<PreparedRequest [GET]>
<!DOCTYPE html>
<html lang="en">

We usually need requests.models.Response r.text property.

r.content.decode() is equivalent to r.text

To finalize this would be the code to get the content from the web page using requests.

import requests
url = ""
    r = requests.get(url) # requests.models.Response
    print(r.text) # print(r.content) if you need bytes
    print("Error with the request")

Get a text file using requests

Here is how to download a file using requests.


import pandas as pd
import io
import requests
bytes_data =requests.get(url).content
text =bytes_data.decode('utf-8') 

with open('cities.csv', 'w') as file:

Output: loaded data

Get a binary file using requests


import requests
r = requests.get(url)
with open('google.logo.png', 'wb') as f:

Get any file using wget


import wget
url = ''
try:, 'google.logo.png')
  print("An exception occurred") 

HTTP response codes

While you create http requests to get the URL content and expect 200 OK it is nice to have this overview of the response status codes you may also get in some cases.

code Short text Long text
100 Continue Request received, please continue
101 Switching Protocols Switching to new protocol; obey Upgrade header
200 OK Request fulfilled, document follows
201 Created Document created, URL follows
202 Accepted Request accepted, processing continues off-line
203 Non-Authoritative Information Request fulfilled from cache
204 No Content Request fulfilled, nothing follows
205 Reset Content Clear input form for further input.
206 Partial Content Partial content follows.
300 Multiple Choices Object has several resources – see URI list
301 Moved Permanently Object moved permanently – see URI list
302 Found Object moved temporarily – see URI list
303 See Other Object moved – see Method and URL list
304 Not Modified Document has not changed since given time
305 Use Proxy You must use proxy specified in Location to access this resource.
307 Temporary Redirect Object moved temporarily – see URI list
400 Bad Request Bad request syntax or unsupported method
401 Unauthorized No permission – see authorization schemes
402 Payment Required No payment – see charging schemes
403 Forbidden Request forbidden – authorization will not help
404 Not Found Nothing matches the given URI.
405 Method Not Allowed Specified method is invalid for this server.
406 Not Acceptable URI not available in preferred format.
407 Proxy Authentication Required You must authenticate with this proxy before proceeding.
408 Request Timeout Request timed out; try again later.
409 Conflict Request conflict.
410 Gone URI no longer exists and has been permanently removed.
411 Length Required Client must specify Content-Length.
412 Precondition Failed Precondition in headers is false.
413 Request Entity Too Large Entity is too large.
414 Request-URI Too Long URI is too long.
415 Unsupported Media Type Entity body in unsupported format.
416 Requested Range Not Satisfiable Cannot satisfy request range.
417 Expectation Failed Expect condition could not be satisfied.
500 Internal Server Error Server got itself in trouble.
501 Not Implemented Server does not support this operation.
502 Bad Gateway Invalid responses from another server/proxy.
503 Service Unavailable The server cannot process the request due to a high load.
504 Gateway Timeout The gateway server did not receive a timely response.
505 HTTP Version Not Supported Cannot fulfill request.

tags: html - https - get request & category: python