Pythonは私たちにurllib
ネットワークリクエストを作成するための標準ライブラリパッケージ。
以下を使用してリクエストを作成します。
from urllib import request
url = 'https://dog.ceo/api/breeds/list/all'
response = request.urlopen(url)
content = response.read()
print(content)
また、使用することができますwith
簡略化するステートメント
from urllib import request
url = 'https://dog.ceo/api/breeds/list/all'
with request.urlopen(url) as response:
content = response.read()
print(content)
応答はバイトのシーケンスです。これは、応答がb''
ストリング:
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.
More python tutorials:
- Introduction to Python
- Installing Python 3 on macOS
- Running Python programs
- Python 2 vs Python 3
- The basics of working with Python
- Python Data Types
- Python Operators
- Python Strings
- Python Booleans
- Python Numbers
- Python, Accepting Input
- Python Control Statements
- Python Lists
- Python Tuples
- Python Sets
- Python Dictionaries
- Python Functions
- Python Objects
- Python Loops
- Python Modules
- Python Classes
- The Python Standard Library
- Debugging Python
- Python variables scope
- Python, accept arguments from command line
- Python Recursion
- Python Nested Functions
- Python Lambda Functions
- Python Closures
- Python Virtual Environments
- Use a GoPro as a remote webcam using Python
- Python, how to create a list from a string
- Python Decorators
- Python Docstrings
- Python Introspection
- Python Annotations
- Python, how to list files and folders in a directory
- Python, how to check if a number is odd or even
- Python, how to get the details of a file
- Python, how to check if a file or directory exists
- Python Exceptions
- Python, how to create a directory
- Python, how to create an empty file
- Python, the `with` statement
- Python, create a network request
- Python, installing 3rd party packages using `pip`
- Python, read the content of a file