当前位置: 首页 > 知识库问答 >
问题:

python3-从url下载pdf文件

乐寒
2023-03-14

我的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,当我下载它是jsut这个html页面

更新:

当我使用Python会话时,从服务器的一个NSWER:

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'

共有1个答案

杜哲彦
2023-03-14

否则,您将必须处理登录过程,以便检索它将(可能)提供给您的身份验证cookie,然后您必须随get请求发送该cookie,以获得预期的pdf。

 类似资料:
  • 我是Python3新手,我正在尝试在登录网站后下载一个文档。 我有2个网址,可以让我立即登录到网页和下载文件。登录后,以下各项: https://www.xxxcompany.com/login.action?loginname=name 这是我的密码。它肯定不起作用,也不会给我打印状态码。我是否误解了一些概念?请帮我解决这个问题。非常感谢你!

  • 我需要一个代码(更喜欢在pyhton3),知道如何认证和下载URL直接从谷歌文档。

  • 我可以通过以下服务下载pdf文件: 但是,当我运行该服务时,我得到了以下响应: %���� 10 obj 要打开pdf,我需要用第二个鼠标按钮点击响应链接: WS网址 然后选择“在新选项卡中打开”以打开pdf文件,看起来您正在运行该服务两次以获得一次pdf文件。 我想自动打开每个请求到WS的pdf文件。 这意味着每次您请求WS时,都应该返回直接在屏幕中打开的pdf文件。 有人能帮我修一下吗? Tk

  • 问题内容: 我正在尝试使浏览器下载从ajax响应接收到的pdf文件。 受jquery ajax下载pdf文件启发,我模拟了如下单击/下载事件: 不幸的是,这仅适用于Chrome,不适用于Firefox + IE。当我尝试在最后两个浏览器中触发它时,没有任何反应。 由于从CMS继承,脚本和标记被放置在iframe中,但是我不确定这是否有影响。 关于如何针对所有现代浏览器进行优化的想法? 问题答案:

  • API将返回CSV/PDF/XLS类型的文件 问题:

  • 当我第一次调用方法时,它成功地下载了文件,但我得到了以下错误: https://madrasty.dev.ibtdi.work/public/reports/15287105135b1e4571f2596-new.pdf(函数)错误:responseSerializationFailed(AlamoFire.Aferror.ResponseSerializationFailureRereason.