- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个网络爬虫可以搜索某些文件并下载它们,但是当出现“另存为或打开”对话框时,我该如何下载 pdf 文件。我目前正在使用 python selenium 进行爬网。这是我的代码。
from selenium import webdriver
import time
browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.tda-sgft.com/TdaWeb/jsp/fondos/Fondos.tda") # Load page
link = browser.find_element_by_link_text("Mortgage Loan")
link.click()
link2 = browser.find_element_by_link_text("ABS")
link2.click()
link3 = browser.find_element_by_link_text("TDA 13 Mixto")
link3.click()
download = browser.find_element_by_link_text("General Fund Information")
download.click()
time.sleep(0.2) # Let the page load, will be added to the API
browser.close()
最佳答案
您将需要修改您的 Firefox 配置文件的首选项。为了让它停止显示该对话框,您需要设置正在使用的配置文件的 browser.helperApps.neverAsk.saveToDisk
属性。为此,您可以这样做(请注意,这是针对 CSV/Excel 文件的 - 我相信您的类型是“应用程序/pdf”):
profile = webdriver.firefox.firefox_profile.FirefoxProfile()
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', ('text/csv,'
'application/csv,'
'application/msexcel'))
对于你的情况(我还没有用 PDF 测试过这个,所以请对它持保留态度 :)),你可以试试这个:
profile = webdriver.firefox.firefox_profile.FirefoxProfile()
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', ('application/pdf'))
第二个参数是一个元组,其中包含永远不会触发 Save As
提示的文件类型。然后将此配置文件传递到您的浏览器
:
browser = webdriver.Firefox(firefox_profile=profile)
现在,当您下载该元组中某种类型的文件时,它应该绕过提示并将其放入您的默认目录中。如果要更改文件下载到的目录,可以使用相同的过程,只需更改几处(在将 profile
附加到浏览器之前执行此操作):
profile.set_preference('browser.download.folderList': 2)
profile.set_preference('browser.download.dir': '/path/to/your/dir')
关于python 网络爬虫下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12099250/
我在一个网站上工作。 我需要了解我的网站是否获得了来自 Google 或任何其他搜索引擎的抓取工具/机器人的访问 在我的应用程序中,我正在拦截 http 请求。并且需要查明爬虫/机器人是否正在发出 h
我与北京理工大学python爬虫的邂逅 在人生的旅途中,我们时常有机会邂逅一些惊艳的事物,它们仿佛一束亮光照亮了前行的道路。而我,一个平凡的人类,也不例外。回想起那个让我心动的时刻,我想起了与北京理工
从“爬虫”谈起 在这个信息爆炸的时代,互联网上的数据宛如茫茫星海,琳琅满目。但是,我们能否想象到这些数据背后隐藏着多少珍贵的信息呢?有人说,要想获得这些数据,我们需要借助一种神奇的力量——爬虫。 什么
探索未知的领域 曾几何时,我陷入了对互联网世界的好奇之中。作为一个普通的人类,我对于计算机技术的了解可谓是一窍不通。然而,好奇心驱使着我踏上了学习的征途。 奇妙的爬虫 在探索计算机领域的过程中,我翻阅
教室的奇妙之旅 在一个晴朗的早晨,我来到了一所神奇且富有魅力的教室。这里充满了未知的冒险和无限的学习可能。嘿,你没听错,就是那个零基础Python视频教程课程的教室!让我带你一起踏上这趟令人期待的爬虫
零基础学python3 爬虫 嗨,各位小伙伴们!今天要和大家分享一段有关学习Python3爬虫的故事。无论是技术小白还是编程高手,都可以从中获益良多。 1. 邂逅神奇的爬虫世界 就像是迷失在未知之地的
我正在开发一个 Python(3) 程序,在该程序中,我必须使用输入作为表示各种操作的多次迭代列表来返回目录迭代的移动次数,例如: ../ 表示移动到当前文件夹的父文件夹。 ./ 保留在同一文件夹中
我和Python:一段关于爬虫和第三方库的故事 曾经有一个人,他和一门编程语言结下了不解之缘。这门语言就是Python,一个灵活而强大的工具,让他的世界变得多姿多彩。 遇见爬虫:探索未知的世界 某天,
偷心技艺:大众点评TOP10 爬虫 Python 故事发生在一个被互联网包围的城市,充满了各种各样的餐馆、咖啡厅和美食街巷。每天都有无数的食客们纠结于选择哪家餐厅才能获得满足和享受。就在这个时候,我偶
登山寻宝:为什么Python爬虫部分网页源码 人生就像登山寻宝的旅程,充满了未知和挑战。而对于爱好编程的我来说,写一个Python爬虫,探索网络世界也是一种刺激的冒险。今天,我想和大家分享一下为什么要
插班生的悲喜交加 记得那个夏天,我踏入了编程的殿堂,成为了一名程序员。对于一个毫无基础的人来说,这是一次勇敢而激动的尝试。然而,更令我兴奋的是,在我的码农之旅中,我发现了一种神奇的力量——Python
一、 意外的宝藏 当我踏进了编程的大门,发现了那个绚丽多彩的世界时,我仿佛捡到了一个意外的宝藏。在编码的世界里,我像是一个魔术师,用着神奇的符号与逻辑来创造奇迹。然而,随着时间的推移,我渐渐找到了一种
“呜呜呜~” 夏日的阳光穿过微风,洒在我身上,仿佛一片温暖而宁静的海洋。我望着眼前充满希望与挑战的屏幕,满脸的期待和困惑。作为一个刚刚入门的编程初学者,我竭尽全力探索着那座神秘的编程世界,好像是航行在
我的编程之旅 嗨,大家好!我是一个普通的人类,对于编程世界充满着无限的好奇和热情。今天,我想向大家讲述一段关于我的编程经历,特别是在爬虫领域的探索之旅。 1. 踏上未知的征途 就像一只踏上大海的小船,
揭秘神奇的爬虫世界:探寻京东销量数据库 从古至今,人类一直渴望了解世界的各个角落。而对于互联网这个充满未知和神秘的领域来说,我们更是怀着好奇和充满冒险精神的心态去探索。今天,我将带领大家踏上一段激动人
啊!你好呀!很高兴能遇到你,让我向你讲一个关于python爬虫爬取关键词排名的故事。 章节一:奇妙的网络世界 网络就像一片浩瀚的海洋,里面有无数宝藏等待着我们发掘。而当我们学会了使用python这把钥
一次意外的奇遇 在我们生活的世界中,技术如今已经无处不在,无论是大型软件开发还是个人应用,都离不开它的支持。而其中最为神奇的领域之一,就是爬虫技术。想象一下,你有可能仅凭几行代码,就能够在电脑屏幕上将
奇遇之旅 在这个大千世界中,我有幸踏上了一次令人兴奋的冒险之旅。故事的开始,是一个寂静而神秘的夜晚。 迷宫的入口 深夜的城市,街道上弥漫着微弱的灯光,仿佛点缀着无数的星星。我沿着曲径通幽的小巷走着,脚
python 爬虫 自动上传下载 从前有一个热爱编程的小伙子,他喜欢利用自己的技术解决各种实际问题。有一天,他面对一个任务:需要在网络上批量上传和下载文件。这可不是一件轻松的工作,但对于这位小伙子来说
一场奇妙的探险之旅:Python编程语言中的爬虫第三方库 故事从一个寻宝者开始。他身穿一件斑驳的皮衣,手持一柄锈迹斑斑的剑,如同一个当代版的荒野游侠。这位勇士的目标是探索Python编程语言中的神秘领
我是一名优秀的程序员,十分优秀!