在python中使用SSL证书访问受保护的网站

时间:2017-09-28 18:16:05

标签: python ssl beautifulsoup

我列出了两组代码 - 一组不起作用(第一组)和另一组(第二组)。问题是如何使用SSL证书使第一个代码工作,因为它在没有验证的情况下不起作用。我有一个用于访问网页的SSL证书。我导出为.pfx(这是唯一的选择)。我如何使用证书凭证访问主页面(第一个代码)。这对我的进步非常有帮助!谢谢。

如果我点击主页面(嵌入了XMLS链接的地方)请求,我得到一个空白的汤(汤。标题是空白的,所以其他汤功能)。这是代码:

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)

1 个答案:

答案 0 :(得分:0)

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

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

此外,您应该从pfx档案(read this for example

中提取证书