我想从网站下载所有 PDF,而不是手动下载,但出现 SSL 错误
I wanted to download all PDFs from a website rather than doing it manually but i get the SSL error
这是我的代码片段,当我 运行 这个 code.I 尝试延迟代码并处理它时,我得到了 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)
。但是 NOT 建议这样做。在 Whosebug 上快速搜索有关生成的错误消息将引导您找到许多有用的线程,这些线程提出了 real 解决方案,例如
这是我的代码片段,当我 运行 这个 code.I 尝试延迟代码并处理它时,我得到了 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)
。但是 NOT 建议这样做。在 Whosebug 上快速搜索有关生成的错误消息将引导您找到许多有用的线程,这些线程提出了 real 解决方案,例如