这是我的代码片段,我在运行此代码时遇到SSL错误。我尝试对代码进行延迟并继续处理,但仍然遇到相同的错误
import os
import requests
from urllib.parse import urljoin
from bs4 import BeautifulSoup
url = "http://bbmp.gov.in/en/covid19bulletins"
#If there is no such folder, the script will create one automatically
folder_location = r'C:\Users\maria.fh\Documents\Automatically downloaded files'
if not os.path.exists(folder_location):os.mkdir(folder_location)
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
session = requests.Session()
retry = Retry(connect=3, backoff_factor=0.8)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
session.get(url)
response = requests.get(url)
soup= BeautifulSoup(response.text, "html.parser")
for link in soup.select("a[href$='.pdf']"):
filename = os.path.join(folder_location,link['href'].split('/')[-1])
with open(filename, 'wb') as f:
f.write(requests.get(urljoin(url,link['href'])).content)
这是引发的SSL异常:
requests.exceptions.SSLError: HTTPSConnectionPool(host='dl.bbmpgov.in', port=443): Max retries exceeded with url: /covid/Covid_Bengaluru_26June_2020%20Bulletin-95%20English.pdf (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)')))
您的客户端(请求)无法验证服务器的SSL证书。您可以将
verify=False
添加到您的呼叫:f.write(requests.get(urljoin(url,link['href']), verify=False).content)
。但是,不建议这样做。有关生成的错误消息的stackoverflow快速搜索将引导您找到许多有用的线程,这些线程提出了real解决方案,例如https://stackoverflow.com/a/57466119/12693728相关问题 更多 >
编程相关推荐