Python 如何刮取受密码保护的ASPX(PDF)页面
我正试图从我们经纪人的网络服务中搜集有关我乐队即将演出的数据(如场地容量、场地地址、设置长度、设置开始时间……) 使用Python3.6和Selenium,我成功地登录到该站点,从主页上抓取了一堆数据,并打开了类似于PDF的ASPX页面。从那以后,我再也无法完成交易了。我已成功地将Selenium驱动程序切换到交易单。但是当我检查该页面时,没有任何内容,只有一个JavaScript脚本列表 我试过Python 如何刮取受密码保护的ASPX(PDF)页面,python,asp.net,pdf,web-scraping,download,Python,Asp.net,Pdf,Web Scraping,Download,我正试图从我们经纪人的网络服务中搜集有关我乐队即将演出的数据(如场地容量、场地地址、设置长度、设置开始时间……) 使用Python3.6和Selenium,我成功地登录到该站点,从主页上抓取了一堆数据,并打开了类似于PDF的ASPX页面。从那以后,我再也无法完成交易了。我已成功地将Selenium驱动程序切换到交易单。但是当我检查该页面时,没有任何内容,只有一个JavaScript脚本列表 我试过 innerHTML = driver.execute_script("return documen
innerHTML = driver.execute_script("return document.body.innerHTML")
…但这会产生相同的脚本列表,而不是我在浏览器中看到的PDF内容
我尝试了这里建议的解决方案:
但解决方案返回的HTML是登录页面,而不是交易单。我的问题不同,因为PDF受密码保护。您将无法使用读取文件,解决方案是:
这个由三部分组成的解决方案适合我: 第1部分(获取受密码保护的PDF的URL) 带有url的元素在页面上可能不同。还建议#嵌入和#内容 第2部分(使用python请求创建一个持久会话,如下所述:。并下载PDF。) 第3部分(根据建议,用
# with selenium
driver.find_element_by_xpath('xpath To The PDF Link').click()
# wait for the new window to load
sleep(6)
# switch to the new window that just popped up
driver.switch_to.window(driver.window_handles[1])
# get the URL to the PDF
plugin = driver.find_element_by_css_selector("#plugin")
url = plugin.get_attribute("src")
# Fill in your details here to be posted to the login form.
# Your parameter names are probably different. You can find them by inspecting the login page.
payload = {
'logOnCode': username,
'passWord': password
}
# Use 'with' to ensure the session context is closed after use.
with requests.Session() as session:
session.post(logonURL, data=payload)
# An authorized request.
f = session.get(url) # this is the protected url
open('c:/yourFilename.pdf', 'wb').write(f.content)