首页 > 解决方案 > Python 请求文本只返回  而不是 HTML

问题描述

我正在尝试抓取文件的链接,以便稍后从网站下载。

我的代码:

outage_page = ' https://www.oasis.oati.com/cgi-bin/webplus.dll?script=/woa/woa-planned-outages-report.html&Provider=MISO '

s = requests.Session()

req = s.get(outage_page, stream=True, verify='我的证书路径在这里')

print(req, '\n', req.headers, '\n', req.raw, '\n', req.encoding, '\n', req.content, '\n', req.text)

这是我得到的输出:

{'Content-Type': 'text/html', 'Content-Encoding': 'gzip', 'Vary': 'Accept-Encoding', 'Server': 'Microsoft-IIS/7.5', 'X-Powered- By': 'ASP.NET', 'X-Content-Type-Options': 'nosniff', 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains','日期':'星期一,2019 年 8 月 26 日 15:48:39 GMT','内容长度':'136'}

ISO-8859-1

b'\xef\xbb\xbf\xef\xbb\xbf\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n \r\n\r\n \r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r \n\r\n\r\n'



进程以退出代码 0 结束

我希望 req.text 返回我可以抓取的 html,但它只返回。其他打印语句仅供参考。我究竟做错了什么?

标签: python-3.xpython-requestsgzip

解决方案


我将继续发布我的解决方案。因此,我将证书文件从 .cer 转换为 .pem,将证书包含在会话中而不是 get 中,并将标头添加到请求中。我将 verify 更改为 false 因为它指的是服务器端证书而不是客户端。

# create the connection
s = requests.Session()
s.cert = 'path/to/cert.pem'
head = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36'
}

req = s.get(outage_page, headers=head, verify=False)

推荐阅读