如何使用 Python 中的 wget 和 curl 选项下载文件?

问题描述

我希望你使用这个 Adyen 请求

$ wget --http-user='[YourReportUser]@Company.[YourCompanyAccount]' --http-password='[YourReportUserPassword]' --quiet --no-check-certificate https://ca-test.adyen.com/reports/download/MerchantAccount/[YourMerchantAccount]/[ReportFileName]

在 Python 中下载文件。如何将wget的options放入urllib2中的request或者requests中?

非常感谢,

解决方法

请求使这变得相当容易:

import requests

r = requests.get('https://ca-test.adyen.com/reports/download/MerchantAccount/[YourMerchantAccount]/[ReportFileName]',auth=('[YourReportUser]@Company.[YourCompanyAccount]','[YourReportUserPassword]'),verify=False)
r.raise_for_status() #fail here if we got something other than 200
#for binary payloads:
with f as open('my file.bin','wb'):
    f.write(r.content)
#or for text:
with f as open('my file.txt','wt'):
    f.write(r.text)

这假设您的端点使用基本身份验证。如果是 Digest Auth,则改为:

r = requests.get('https://ca-test.adyen.com/reports/download/MerchantAccount/[YourMerchantAccount]/[ReportFileName]',auth=requests.HTTPDigestAuth('[YourReportUser]@Company.[YourCompanyAccount]',verify=False)

注意 verify=False 参数告诉请求不要检查 TLS 证书。如果您有需要用于验证的证书,也可以设置 verify=/path/to/certfile。有关详细信息,请参阅 https://requests.readthedocs.io/en/master/user/advanced/#ssl-cert-verification

请求文档非常好:https://requests.readthedocs.io/en/master/