【问题标题】:Python requests text only returning  instead of HTMLPython 请求文本只返回  而不是 HTML
【发布时间】:2019-08-26 15:54:48
【问题描述】:

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

我的代码:

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.文字)

这是我得到的输出:

{'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', 'Date': 'Mon, 26 Aug 2019 15:48:39 GMT', 'Content-Length': '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.x python-requests gzip


    【解决方案1】:

    我将继续发布我的解决方案。因此,我将证书文件从 .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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      • 2016-07-29
      • 1970-01-01
      • 2020-07-30
      • 2011-03-15
      • 2013-04-09
      • 2023-04-05
      相关资源
      最近更新 更多