Use Google Takeout . It is the official "clean" way to get your data.
For those who need a custom solution or want to automate a recurring backup, a simple Python script using the imaplib and email libraries can fetch mail and save attachments to a specific directory. gmail download mail with attachments
There are several scripts and tools available that scan your "Sent" or "Inbox" labels and pull every attachment into a single folder. 5. Using Python for Tech-Savvy Users Use Google Takeout