【发布时间】: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