【问题标题】:Accessing secured website using SSL certificate in python在 python 中使用 SSL 证书访问安全网站
【发布时间】:2018-03-10 14:11:09
【问题描述】:

我列出了两组代码 - 一组不起作用(第一组),另一组起作用(第二组)。问题是如何使第一个代码使用 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)

【问题讨论】:

    标签: python ssl beautifulsoup


    【解决方案1】:

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

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

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

    【讨论】:

    • Roman - 我能够导出 .cer(不是 .cert)文件,但不能导出密钥。该示例没有讨论 .key 文件。你能帮忙吗?
    • 试试requests.get(url, verify='/path/to/certfile')。可能我误解了你想要什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    • 2019-01-23
    • 1970-01-01
    • 2018-01-21
    相关资源
    最近更新 更多