【发布时间】:2016-06-21 19:38:43
【问题描述】:
问题
我正在尝试从一个建立在一个古怪的旧大型机上的网站下载 PDF 文件,为了支持流量,该网站实施了等待页面。等待页面将呈现,您将花几秒钟时间查看它而不是您想要的 PDF,然后它会消失,您会去您想去的地方。
这是我的场景:
- 我转到页面。
- 可能有 33% 的时间,我得到了等待页面。这是等待页面代码:
<div id="wrapper">
<p><hr /></p>
</p>
<div id="waiting-main">
<p style="text-align: center; margin: 6px 0 15px 0;"><img src="/ns_images2/doblogo_1.jpg" border="0" />
</p>
<p style="text-align: center; font-size: 30px; line-height: 34px;">Just a moment</p>
<p style="text-align: left; color: #525252; font-size: 20px; line-height: 22px;">
Your request is being processed.</br></br>
Due to the high demand it may take a little longer. You will be directed to the page shortly. Please do not leave this page. Refreshing the page will delay the response time. We apologize for the delay.</br></br>
...[snipped for brevity]...
</p>
</div>
</div>
</body></html>
- 等待页面退出,我加载以下 HTML:
<html><body marginwidth="0" marginheight="0" style="background-color: rgb(38,38,38)"><embed width="100%" height="100%" name="plugin" src="http://a810-bisweb.nyc.gov/bisweb/CofoDocumentContentServlet?passjobnumber=null&cofomatadata1=cofo&cofomatadata2=M&cofomatadata3=000&cofomatadata4=092000&cofomatadata5=M000092531.PDF&requestid=5" type="application/pdf"><div id="annotationContainer"><style>#annotationContainer { overflow: hidden; position: absolute; pointer-events: none; top: 0; left: 0; right: 0; bottom: 0; display: -webkit-box; -webkit-box-align: center; -webkit-box-pack: center; } .annotation { position: absolute; pointer-events: auto; } textarea.annotation { resize: none; } input.annotation[type='password'] { position: static; width: 200px; margin-top: 100px; } </style></div></body></html>
- 我在本地下载 PDF 文档。结束!
我尝试的解决方案
不知道 selenium 并不真正支持 PDF(或者支持吗?),这是我的方法:
_driver = webdriver.PhantomJS()
...
req_string = ...[a very long URL]...
_driver.get(req_str)
...
try:
WebDriverWait(_driver, 10).until(
# Cannot use:
# lambda a: not a.presence_of_element_located((By.ID, "waiting-main"))
# Because:
# https://blog.mozilla.org/webqa/2012/07/12/how-to-webdriverwait/
# Which suggests this working alternative.
lambda s: len(s.find_elements(By.ID, "waiting-main")) == 0
)
finally:
_driver.save_screenshot("test.png") # Maybe?
# How do I get the actual PDF code? :/
问题
我看不出用硒做这件事的方法。所以我的问题是:
如何加载页面,等待等待页面,然后使用 Python (2.7) 下载随后提供的 PDF?
或者,如果这个 可以使用 selenium,我该怎么做?
例子
The link on this page exemplifies my problem.
解决方法
目前我正在使用:
r = requests.get(req_str)
while "waiting-main" in r.text:
time.sleep(5)
r = requests.get(req_str)
目前还没有关于它的效果如何......
页面
【问题讨论】:
-
您可以使用
WebDriverWait(driver, 10).until_not(something_to_disappear)等待加载程序窗口关闭。至于第二部分,我不确定我是否理解正确"PDF that comes afterwards"...你的意思是什么? -
不幸的是,我的描述因对我要解决的问题的不完全理解而受到阻碍。我已对其进行了更新,以尝试使我的问题更加清晰 - 欢迎提供反馈!
-
如果您仔细观察,您会注意到pdf 实际上有一个您可以直接点击的网址。如果你能弄清楚那个 URL 是如何构造的,你就可以缩短整个过程。
-
这正是我点击的链接,实际上;
CofoDocumentContentServlet偶尔会提供等待通知。
标签: python http selenium pdf download