example

    1. import requests
    2. # 通过requests库下载文件
    3. url = 'https://www.gipsa.usda.gov/fgis/exportgrain/CY2016.csv'
    4. r = requests.get(url)
    5. print(r.content)
    6. with open("myCY2016.csv", "wb") as code:
    7. code.write(r.content)

    ssl 问题
    https://docs.python-requests.org/en/latest/user/advanced/#ssl-cert-verification

    1. File "C:\Users\admin\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\adapters.py", line 497, in send
    2. raise SSLError(e, request=request)
    3. requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645)

    看到stackoverflow中说有这样:

    1. The problem you are having is caused by an untrusted SSL certificate.
    2. Like @dirk mentioned in a previous comment, the *quickest* fix is setting verify=False
    3. .
    4. Please note that this will cause the certificate not to be verified. **This will expose your application to security risks, such as man-in-the-middle attacks.**
    5. Of course, apply judgment. As mentioned in the comments, this *may* be acceptable for quick/throwaway applications/scripts, *but really should not go to production software*.
    6. If just skipping the certificate check is not acceptable in your particular context, consider the following options, your best option is to set the verify
    7. parameter to a string that is the path of the .pem
    8. file of the certificate (which you should obtain by some sort of secure means).
    9. So, as of version 2.0, the verify
    10. parameter accepts the following values, with their respective semantics:
    11. True
    12. : causes the certificate to validated against the library's own trusted certificate authorities (Note: you can see which Root Certificates Requests uses via the Certifi library, a trust database of RCs extracted from Requests: [Certifi - Trust Database for Humans](http://certifiio.readthedocs.org/en/latest/)).
    13. False
    14. : bypasses certificate validation *completely*.
    15. Path to a CA_BUNDLE file for Requests to use to validate the certificates.
    16. Source: [Requests - SSL Cert Verification](http://docs.python-requests.org/en/master/user/advanced/?highlight=ssl#ssl-cert-verification)
    17. Also take a look at the cert
    18. parameter on the same link.

    好的,虽然不是很明白,但是把参数verify = False设置好。继续。

    1. C:\Users\admin\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\packages\urllib3\connectionpool.py:843:
    2. InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised.
    3. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
    4. InsecureRequestWarning)

    又看到stackoverflow看到这个:

    1. The reason doing urllib3.disable_warnings()
    2. didn't work for you is because it looks like you're using a separate instance of urllib3 vendored inside of requests.
    3. I gather this based on the path here: /usr/lib/python2.6/site-packages/requests/packages/urllib3/connectionpool.py
    4. To disable warnings in requests' vendored urllib3, you'll need to import that specific instance of the module:
    5. import requestsfrom requests.packages.urllib3.exceptions import InsecureRequestWarningrequests.packages.urllib3.disable_warnings(InsecureRequestWarning)

    完成

    1. import requests
    2. from requests.packages.urllib3.exceptions import InsecureRequestWarning
    3. requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
    4. # 通过requests库下载文件
    5. url = 'https://www.gipsa.usda.gov/fgis/exportgrain/CY2016.csv'
    6. r = requests.get(url,verify = False)
    7. print(r.content)
    8. with open("myCY2016.csv", "wb") as code:
    9. code.write(r.content)