【问题标题】:Need to download the PDF, NOT the content of the webpage需要下载PDF,而不是网页的内容
【发布时间】:2018-05-18 11:05:56
【问题描述】:

就目前而言,我能够获取 PDF 链接 EXAMPLE OF THE LINK HERE 的网页内容,但我不想要网页的内容,我想要 PDF 的内容,所以我可以将内容放入我电脑上文件夹中的 PDF。

我已经成功地在不需要登录且没有代理服务器的网站上执行此操作。

相关代码:

import os
import urllib2
import time
import requests
import urllib3
from random import *


s = requests.Session()
data = {"Username":"username", "Password":"password"}
url = "https://login.url.com"

print "doing things"
r2 = s.post(url, data=data, proxies = {'https' : 'https://PROXYip:PORT'}, verify=False)

#I get a response 200 from printing r2
print r2


downlaod_url = "http://msds.walmartstores.com/client/document?productid=1000527&productguid=54e8aa24-0db4-4973-a81f-87368312069a&DocumentKey=undefined&HazdocumentKey=undefined&MSDS=0&subformat=NAM"

file = open("F:\my_filepath\document" + str(maxCounter) + ".pdf", 'wb')
temp = s.get(download_url, proxies = {'https' : 'https://PROXYip:PORT'}, verify=False)

#This prints out the response from the proxy server (i.e. 200)
print temp

something = uniform(5,6)
print something
time.sleep(something)

#This gets me the content of the web page, not the content of the PDF
print temp.content

file.write(temp.content)
file.close()

我需要帮助了解如何“下载”PDF 的内容

【问题讨论】:

  • 您试图在您提供的 URL 中下载 PDF 还是我误解了您?因为我这样做没有问题
  • @EvyatarMeged 是的,这就是我想做的,我想下载 PDF。哦,糟糕,我忘了包含 download_url 声明

标签: python python-2.7 proxy python-requests


【解决方案1】:

试试这个:

import requests

url = 'http://msds.walmartstores.com/client/document?productid=1000527&productguid=54e8aa24-0db4-4973-a81f-87368312069a&DocumentKey=undefined&HazdocumentKey=undefined&MSDS=0&subformat=NAM'

pdf = requests.get(url)
with open('walmart.pdf', 'wb') as file:
    file.write(pdf.content)

编辑

再次尝试使用请求会话来管理 cookie(假设它们在登录后向您发送),也可能使用不同的代理

proxy_dict = {'https': 'ip:port'}

with requests.Session() as session:
    # Authentication request, use GET/POST whatever is needed
    # data variable should hold user/password information
    auth = session.get(login_url, data=data, proxies=proxy_dict, verify=False)
    if auth.status_code == 200:
        print(auth.cookies) # Tell me if you got anything
        pdf = auth.get('download_url')  # Were continuing the same session
        with open('walmart.pdf', 'wb') as file:
            file.write(pdf.content)
    else:
        print('No go, got {0} response'.format(auth.status_code))

【讨论】:

  • 不幸的是,这对我不起作用,我将编辑您的答案以向您展示您修改后的代码以适合我的程序
  • 在请求中不包含 data=data 的情况下,我得到一个响应 200,但是当我在文件夹中打开 PDF 时出现错误:“无法打开“walmart.pdf”因为它不是受支持的文件类型或文件已损坏”
  • 您不需要在获取请求中发送任何数据来获取 PDF 文件。如果我理解正确,您通过 POST 对某个网站进行身份验证?我会在一分钟内编辑我的答案以尝试适合你的答案。
  • 不行,我得到一个响应411,我一直在做一些阅读,我可能会切换到urllib3
猜你喜欢
  • 1970-01-01
  • 2020-11-12
  • 1970-01-01
  • 2014-01-05
  • 1970-01-01
  • 2017-08-10
  • 1970-01-01
  • 2015-03-23
  • 1970-01-01
相关资源
最近更新 更多