Python3从Web下载文档

时间:2018-10-05 07:25:48

标签: python-3.x download xmlhttprequest python-requests

我是Python3的新手,并且尝试登录网站后下载文档。

我有2个网址,可以让我立即登录该页面并下载文档。登录后为:

  1. https://www.xxxcompany.com/login.action?loginname=name&password=psw
  2. https://www.xxxcompany.com/doc_download_all.action?ID=37887&edition=PD&Year=2018&Month=10&Day=5&&CLI=&transferNumber=&inOut=C&deviceType=A&minDuration=0&maxDuration=0&sortType=0&sortAsc=1&showAdv=0&viewtype=0&subPage=M&RMID=-1&updateRMID=&updateRecordID=&customField1=

这是我的代码。它绝对不起作用,并且不会显示状态代码。我误会了一些概念吗?请帮助我解决问题。非常感谢!

from lxml import html
import webbrowser
import requests
def login():
        with requests.session() as s:
        # fetch the login page
                s.get(url1)
                print(s.status_code) #check whether its successfully login
                s.get(url2) #download the doc

1 个答案:

答案 0 :(得分:0)

您需要将数据写入文件。

url = "http://www.xxxx.com/xxx/xxxx/sample.doc"
import requests
with requests.Session() as se:
    req = se.get(url)
    with open(url.split("/")[-1],"wb") as doc:
        doc.write(req.content)