证书SSLv3警报握手失败的URLLib

2024-04-26 23:45:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Python3.7.3和requests_pkcs12库来抓取一个我必须传递证书和密码的网站,然后从页面上的链接下载并提取zip文件。我已经把第一部分做好了。但是当我尝试使用urllib读取文件时,我得到了一个错误

import urllib.request
from bs4 import BeautifulSoup
import requests
from requests_pkcs12 import get

# get page and setup BeautifulSoup
# r = requests.get(url) # old non-cert method
r = get(url, pkcs12_filename=certpath, pkcs12_password=certpwd)

# find zip files to download
soup = BeautifulSoup(r.content, "html.parser")

# Read files
i = 1
for td in soup.find_all(lambda tag: tag.name=='td' and tag.text.strip().endswith('DAILY.zip')):
    link = td.find_next('a')
    print(td.get_text(strip=True), link['href'] if link else '')  # good
    zipurl = 'https:\\my.downloadsite.com" + link['href'] if link else ''
    print (zipurl)  # good
    # Read zip file from URL    
    url = urllib.request.urlopen(zipurl)  # ERROR on this line SSLv3 alert handshake failure
    zippedData = url.read()

我已经看过很多关于Python2.x的老文章,但是我想知道现在用Python3.7.x中的新库做这件事的最好方法是什么

下面是错误的堆栈跟踪

Here is the stack of the error:


Tags: 文件fromimporturlgettaglinkfind
1条回答
网友
1楼 · 发布于 2024-04-26 23:45:53

答案是不使用urllib,而是使用相同的请求替换,允许向其传递pfx和密码

最后两行:

url = urllib.request.urlopen(zipurl)  # ERROR on this line SSLv3 alert handshake failure
zippedData = url.read()

应替换为:

url = get(zipurl, pkcs12_filename=certpath, pkcs12_password=certpwd)
zippedData = url.content

相关问题 更多 >