首页 > 解决方案 > Python SSL Cert error despite no browser expiry

问题描述

I am a python noob and was trying to scrape random websites (without abusing). This site caught my attention and my code ran:-

import requests

url = 'https://resultsarchives.nic.in/cbseresults/cbseresults2018/class12zpq/class12th18.asp'
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "Accept-Language": "en-US,en;q=0.9,bn;q=0.8",
    "X-Requested-With": "XMLHttpRequest",
    "Content-Type": "application/x-www-form-urlencoded",
    ":authority": "resultsarchives.nic.in",
    "Origin": "http://resultsarchives.nic.in",
    "Referer": "https://resultsarchives.nic.in/cbseresults/cbseresults2018/class12zpq/class12th18.htm",
    "sec-fetch-dest": "document",
    "sec-fetch-mode": "navigate",
    "sec-fetch-site": "same-origin",
    "sec-fetch-user": "?1",
    "upgrade-insecure-requests": "1"
}
x = range(8397, 8398)
i=0
for i in x:
    payload = {
        'regno': '6529437',
        'sch': '12345',
        'cno': str(i),
        'B2': 'Submit'
    }
    response = requests.post(url, headers=headers, data=payload)
    open('scrape.html', 'a', encoding="utf-8").write(response.text)

When executed, an SSL Certificate Expiry Error is thrown. However, browsers (chrome/firefox) work fine, and note the certificate to expire in Dec. 2022. The error ran:

requests.exceptions.SSLError: HTTPSConnectionPool(host='resultsarchives.nic.in', port=443): Max retries exceeded with url: /cbseresults/cbseresults2018/class12zpq/class12th18.asp (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1129)')))

If I process the same code using the Http version(s) of the site(s), it works fine!

标签: pythonpython-requestsssl-certificate

解决方案


推荐阅读