The blockchain.com link given in the accepted answer no longer works.
From the published API
https://blockchain.info/api/blockchain_api
The URL you need to download a HEX formatted block is as follows.
https://blockchain.info/rawblock/$block_hash
This gives you a hex formatted file that can be converted to binary with xxd -r -p
as above.
The bitcoin block header is quite straightforward.
https://en.bitcoin.it/wiki/Protocol_documentation#Block_Headers
| Size | Desc | Type | Comments |
| --- | -- | -- | -- |
| 4 | version | int32_t | Block version information (note, this is signed) |
| 32 | prev_block | char[32] | The hash value of the previous block this particular block references |
| 32 | merkle_root | char[32] | The reference to a Merkle tree collection which is a hash of all transactions related to this block |
| 4 | timestamp | uint32_t | A timestamp recording when this block was created (Will overflow in 2106[2]) |
| 4 | bits | uint32_t | The calculated difficulty target being used for this block |
| 4 | nonce | uint32_t | The nonce used to generate this block… to allow variations of the header and compute different hashes |
To view the nonce:
>>> data = open('block.bin', 'rb').read()
>>> nonce = data[4+32+32+4+4:][:4]
>>> nonce.hex()
'6d1daf7b'
To calculate the hash code for the header:
>>> import hashlib
>>> from binascii import unhexlify, hexlify
>>> header = data[:4+32+32+4+4+4]
>>> hash = hashlib.sha256(hashlib.sha256(header).digest()).digest()
>>> hexlify(hash).decode("utf-8")
'83331a9f1032273394480e8b9d55bc9be72117645a26ff000000000000000000'
>>> hexlify(hash[::-1]).decode("utf-8")
'000000000000000000ff265a641721e79bbc559d8b0e4894332732109f1a3383'
This above is from:
https://en.bitcoin.it/wiki/Block_hashing_algorithm
The following blogpost by Andrej Karpathy's has more information about how you can go about verifying the transactions in the block.
https://karpathy.github.io/2021/06/21/blockchain/