Python, create a network request

Python offers us the urllib standard library package to create network requests.

Create a request using:

from urllib import request
url = 'https://dog.ceo/api/breeds/list/all'

response = request.urlopen(url)
content = response.read()

print(content)

You can also use the with statement to simplify

from urllib import request
url = 'https://dog.ceo/api/breeds/list/all'

with request.urlopen(url) as response:
   content = response.read()

print(content)

The response is a sequence of bytes, as you will notice because the response is wrapped in a b'' string:

b'{"message":{"affenpinscher":[],"african":[],"airedale":[],"akita":[],"appenzeller":[],"australian":["shepherd"],"basenji":[],"beagle":[],"bluetick":[],"borzoi":[],"bouvier":[],"boxer":[],"brabancon":[],"briard":[],"buhund":["norwegian"],"bulldog":["boston","english","french"]},"status":"success"}'

Decode it to a UTF-8 encoded string using content.decode('utf-8')

This gets the HTML content from my website flavicopes.com:

from urllib import request

url = 'https://flaviocopes.com'

with request.urlopen(url) as response:
   content = response.read().decode('utf-8')

print(content)

You can parse the response as JSON, using the json standard library module:

from urllib import request
import json

url = 'https://dog.ceo/api/breeds/list/all'

with request.urlopen(url) as response:
   content = response.read()

data = json.loads(content)
print(data['status'])

If you need to specify query parameters, use the urllib.parse() method to build the query string:

from urllib import request, parse
url = 'https://api.thecatapi.com/v1/images/search'

parms = {
    'limit' : 5,
    'page' : 1,
    'order' : 'Desc'
}

querystring = parse.urlencode(parms)

with request.urlopen(url + '?' + querystring) as response:
   content = response.read().decode('utf-8')

print(content)

This is the built-in urllib package.

For convenience purposes, you might want to use the requests package, not part of the Python standard library but quite popular.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

Python offers us the urllib standard library package to create network requests.

Create a request using:

from urllib import request
url = 'https://dog.ceo/api/breeds/list/all'

response = request.urlopen(url)
content = response.read()

print(content)

You can also use the with statement to simplify

from urllib import request
url = 'https://dog.ceo/api/breeds/list/all'

with request.urlopen(url) as response:
   content = response.read()

print(content)

The response is a sequence of bytes, as you will notice because the response is wrapped in a b'' string:

b'{"message":{"affenpinscher":[],"african":[],"airedale":[],"akita":[],"appenzeller":[],"australian":["shepherd"],"basenji":[],"beagle":[],"bluetick":[],"borzoi":[],"bouvier":[],"boxer":[],"brabancon":[],"briard":[],"buhund":["norwegian"],"bulldog":["boston","english","french"]},"status":"success"}'

Decode it to a UTF-8 encoded string using content.decode('utf-8')

This gets the HTML content from my website flavicopes.com:

from urllib import request

url = 'https://flaviocopes.com'

with request.urlopen(url) as response:
   content = response.read().decode('utf-8')

print(content)

You can parse the response as JSON, using the json standard library module:

from urllib import request
import json

url = 'https://dog.ceo/api/breeds/list/all'

with request.urlopen(url) as response:
   content = response.read()

data = json.loads(content)
print(data['status'])

If you need to specify query parameters, use the urllib.parse() method to build the query string:

from urllib import request, parse
url = 'https://api.thecatapi.com/v1/images/search'

parms = {
    'limit' : 5,
    'page' : 1,
    'order' : 'Desc'
}

querystring = parse.urlencode(parms)

with request.urlopen(url + '?' + querystring) as response:
   content = response.read().decode('utf-8')

print(content)

This is the built-in urllib package.

For convenience purposes, you might want to use the requests package, not part of the Python standard library but quite popular.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-01-31T05:00:00+00:00) Python, create a network request. Retrieved from https://www.scien.cx/2021/01/31/python-create-a-network-request/

MLA
" » Python, create a network request." flaviocopes.com | Sciencx - Sunday January 31, 2021, https://www.scien.cx/2021/01/31/python-create-a-network-request/
HARVARD
flaviocopes.com | Sciencx Sunday January 31, 2021 » Python, create a network request., viewed ,<https://www.scien.cx/2021/01/31/python-create-a-network-request/>
VANCOUVER
flaviocopes.com | Sciencx - » Python, create a network request. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/01/31/python-create-a-network-request/
CHICAGO
" » Python, create a network request." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/01/31/python-create-a-network-request/
IEEE
" » Python, create a network request." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/01/31/python-create-a-network-request/. [Accessed: ]
rf:citation
» Python, create a network request | flaviocopes.com | Sciencx | https://www.scien.cx/2021/01/31/python-create-a-network-request/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.