微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何使用 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/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。