python - 在 python 中使用 SSL 证书访问 protected 网站

标签 python ssl beautifulsoup

我列出了两组代码 - 一组不起作用(第一个),另一组起作用(第二个)。问题是我怎样才能使第一个代码使用 SSL 证书工作,因为它没有验证就无法工作。我有一个用于访问网页的 SSL 证书。我导出为 .pfx(这是唯一的选择)。我将如何使用证书凭据访问主页(第一个代码)。这将极大地帮助我的进步!谢谢。

如果我通过请求访问主页(其中嵌入了 XMLS 链接),我得到的是空白汤(soup.title 是空白,其他汤功能也是如此)。这是代码:

from bs4 import BeautifulSoup
import requests

url = 'https://www.oasis.oati.com/cgi-bin/webplus.dll?script=/woa/woa-planned-outages-report.html&Provider=MISO'
response = requests.get(url, verify=False)
soup = BeautifulSoup(response.content, "html.parser")

但是,如果我直接点击带有特定 XML 链接的页面(没有验证),我可以使用以下代码检索它:

import requests
import xml.etree.ElementTree as ET
import pandas as pd

url = 'https://www.oasis.oati.com/woa/docs/MISO/MISODocs/CurrentData/2308_Planned_Outages_2017-09-19-18-50-00.xml'
response = requests.get(url, verify=False)
root=ET.fromstring(response.text)

all_records = [] #This is our record list which we will convert into a dataframe
for i, child in enumerate(root): #Begin looping through our root tree
    record = {} #Place holder for our record
    for subchild in child: #iterate through the subchildren to user-agent, Ex: ID, String, Description
        record[subchild.tag] = subchild.text #Extract the text create a new dictionary key, value pair
        all_records.append(record) #Append this record to all_records

df = pd.DataFrame(all_records).drop_duplicates().reset_index(drop=True)

最佳答案

您应该在 request.get 调用中使用 cert 选项:

requests.get(url, cert=('/path/client.cert', '/path/client.key'))

此外,您应该从 pfx 存档中提取您的证书 (read this for example)

关于python - 在 python 中使用 SSL 证书访问 protected 网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46475816/

相关文章:

python - "save() got an unexpected keyword argument ' 在功能 View 中提交 '"错误

python - 请求模块抛出 OpenSSL.SSL.Error

java - 两种双向 SSL 身份验证

apache - 在 Apache 上配置双向客户端 Auth SSL 证书

python - python中for行的语法错误

python - 为什么我的 Python 中的 def 函数不起作用?

python - Pandas :按组大小和数据值过滤

python - 每天在两列范围之间的数据框中添加行

python - 在 Python 中定义一个急切求值的 lambda 列表

Python BeautifulSoup 在写入文件时创建奇怪的\xe2 unicode 字符