【问题标题】:python3 - download pdf file from urlpython3 - 从url下载pdf文件
【发布时间】:2020-11-04 18:07:37
【问题描述】:

我的python3代码:

import requests

url = sys.argv[1]
r = requests.get(url, stream=True)
chunk_size = 20000
with open('metadata.pdf', 'wb') as fd:
    for chunk in r.iter_content(chunk_size):
        fd.write(chunk)

它将内容保存在 metadat.pdf 中,但这不是 pdf 的真实内容,它是这个 html 页面:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<!-- $HTMLid:   index.html /main/6 11-Jun-2004.13:54:09 $ -->
<head>
<title>Allied Waste</title>

<script language="JavaScript">
<!--
if (top != self) {
        top.location = self.location;
    }
function doRedirect() {
  document.login.submit();
} 

function init () {
    var initChar = /^\?/;
    var list = top.location.search.replace(initChar,"");
    var parms = list.split('&');
    for ( ct=0; ct < parms.length; ct++ ) {
        vals = parms[ct].split('=');
        switch ( vals[0] ) {
            case "unitCode":
                document.login.unitCode.value = unescape(vals[1]);
                if ( document.login.unitCode.value == 'undefined' || document.login.unitCode.value == '' )
                    document.login.unitCode.value = "ALW";
                break;
      default:
        document.login.unitCode.value = "ALW";
                break;
        }
    }
    document.login.submit();
}
//-->
</script>
</head>
<body onload="init()">
  <form name="login" action="inetSrv" method="post">
    <input type="hidden" name="type" value="SignonService"/>
    <input type="hidden" name="action" value="SignonPrompt"/>
    <input type="hidden" name="client" value="701122300"/>
    <input type="hidden" name="unitCode" value=""/>
  </form>
</body>
</html>

任何帮助,我如何保存文件的真实内容,而不是这个 html? 它应该是真正的pdf,当我下载它时,它就是这个html页面

更新:

当我使用 python 会话时来自服务器的答案:

b'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\n<html>\n\n                                                                                                              \n<head><title></title>\n                     \n<LINK REL="StyleSheet" HREF="styles/mainStyle.css">\n</head>\n\n<body>\n<div style="float: left; border: 1px solid black; background-color: #FFFFFF; padding: 5px">\n\t<div class="TitleFont">Operation failed</div>\n\t<div class="TitleFont">Reason</div>\n\t<div>\n\t<div class="custom-message-box">\n\t\t\t\t<div class="ErrorFont" ALIGN="left" >A server error has occurred.</div>\n\t\t\t\t<div class="ErrorFont" ALIGN="left" >Error reference id: DLY-00716</div>\n\t\t\t\t<div class="ErrorFont" ALIGN="left" >Time: Wed Jul 15 05:33:12 CDT 2020</div>\n\t</div>\n\t</div>\n\t<div style="width: 600px">\n\t\t<p class="form-style-text">\n\t\tIf contacting customer support, please quote the above error reference id. You may be able to press the browser Back button to return to the previous screen. Otherwise you may need to login again. We apologize for the inconvenience.\n\t\t</p>\n\t</div>\n</div>\n\n</body>\n</html>\n\n'

【问题讨论】:

标签: html python-3.x pdf python-requests


【解决方案1】:

看起来该页面是对登录页面的重定向。如果可以的话,手动操作可能会更简单。

否则,您将必须处理登录过程才能检索它(可能)将提供给您的身份验证 cookie,然后您必须将其与 get 请求一起发送以使预期的 pdf 可用。

【讨论】:

  • 我已经有登录详细信息,我在脚本中创建了登录过程,我也有 cookie.txt 文件,但我不确定我需要如何传递它
  • 使用会话(简单):stackoverflow.com/a/31571805/11384184 或使用 cookie 文件:stackoverflow.com/a/31555440/11384184
  • 我已经有 cookie.txt 并且当我这样做时 response = requests.request("GET", url, cookies='cookie.txt') print(response.text.encode('utf8' )) 它不工作
  • cookies 参数值必须是 cookie 内容(它是事物的 dict),所以你必须阅读它,并且根据你在这里的内容可能会对其进行调整。这就是使用会话更简单的原因。
  • 我是这样做的,我收到了新的答案,请看更新
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-16
  • 2018-06-22
  • 2023-02-17
  • 1970-01-01
  • 2019-07-14
  • 2017-08-13
  • 2014-11-14
相关资源
最近更新 更多