The blockchain.com link given in the accepted answer no longer works.
From the published API
The URL you need to download a HEX formatted block is as follows.
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.
| Size | Desc | Type | Comments |
| --- | -- | -- | -- |
| 4 | version | int32_t | Block version information (note, this is signed) |
| 32 | prev_block | char | The hash value of the previous block this particular block references |
| 32 | merkle_root | char | 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) |
| 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]
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()
This above is from:
The following blogpost by Andrej Karpathy's has more information about how you can go about verifying the transactions in the block.