爬虫之自动保存文档-使用python/selenium
来源:互联网 发布:日历设计软件 编辑:程序博客网 时间:2024/06/07 17:25
网络抓取的时候会碰到需要从网站下载文件的情况。下面提供两种方法:
1. selenium + firefox + firefoxProfile
核心要点是在firefox中设置相关的下载参数,然后在模拟点击的时候,selenium webdriver会自动保存对应的文档。
貌似文件的自动保存需要使用的webdriver只能是firefox。在网上并没有搜到使用对其他webdriver工具的支持资料。
selenium的自动存档可以参考《Docs-Selenium Python Bindings》(github上的一个python-selenium的使用文档,很靠谱)中的常见问题How to auto save files using custom Firefox profile
prof = webdriver.FirefoxProfile();# 下面几个是核心参数prof.set_preference("browser.download.folderList", 2) # 2表示自定义文件夹 0表示保存到桌面prof.set_preference("browser.download.manager.showWhenStarting",False) # 没什么用prof.set_preference("browser.download.dir","D:\\selenium\\") # 设置默认的保存文件夹# 设置自动保存的文件类型,如果firefox不能自动保存,一定是文件类型不对</span>prof.set_preference("browser.helperApps.neverAsk.saveToDisk", 'application/a-gzip,application/x-gzip')# 其他可选文件类型"application/x-gzip;application/zip,application/x-gtar,text/plain,application/x-compressed,application/octet-stream,application/pdf") # 下面这些参数是从别的地方看到的prof.set_preference("browser.helperApps.alwaysAsk.force", False) prof.set_preference("browser.download.manager.alertOnEXEOpen", False) prof.set_preference("browser.download.manager.focusWhenStarting", False)prof.set_preference("browser.download.useDownloadDir", True) prof.set_preference("browser.download.manager.alertOnEXEOpen", False) prof.set_preference("browser.download.manager.closeWhenDone", True) prof.set_preference("browser.download.manager.showAlertOnComplete", False) prof.set_preference("browser.download.manager.useWindow", False) ##disable Firefox's built-in PDF viewer prof.set_preference("pdfjs.disabled",True)# 给firefox使用 driver = webdriver.Firefox(firefox_profile=prof)# 直接点击下载按钮,文件就会自动保存了driver.get(url)driver.find_element_by_css_selector(".primary-").click()
文件的MIME类型可以参考wiki《List of archive formats》
2. urllib2直接保存
使用urllib2直接读取请求的url,urllib2可以保存网页返回的结果,直接写文件就可以达到保存的目的
try: url = 'https://reportingitc2.apple.com/api/report?vendorID=85865236&reportType=2A&endDate=2015%2F11%2F24&vendorType=1&CSRF=' + self.header_csrf print url #待访问的url request = urllib2.Request(url)# request.add_header('Accept-encoding', '"gzip, deflate') request.add_header('Cookie', self.cookie_header) #添加cookie request.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36')# request.add_header('Host', 'itunesconnect.apple.com') try: pageText = urllib2.urlopen(request, timeout=30).read() open('D:/selenium/b.txt.gz', "wb").write(pageText) #保存返回的下载文件,如果是压缩文件,使用wb,而不是w print "[succ]download gz_file successfully." except urllib2.URLError, e: print e except urllib2.URLError, e: print e
0 0
- 爬虫之自动保存文档-使用python/selenium
- Python爬虫之selenium
- Python爬虫Selenium使用
- python爬虫实战--selenium验证码保存+多线程多标签+自动点击+完整代码
- Python爬虫工具之Selenium
- Python爬虫小结之Selenium
- python爬虫----初次使用selenium
- Python爬虫利器之Selenium的用法
- Python爬虫之Selenium+PhantomJS组合
- Python爬虫学习之Selenium详解
- Python爬虫利器之selenium的安装
- Python爬虫利器之selenium常用操作
- Python爬虫利器之selenium有无界面
- python写爬虫6-selenium的使用
- Python 爬虫之 selenium 爬虫,模拟浏览器爬取天猫信息
- 【python 爬虫】linux 下 selenium+phantomjs 自动模拟登陆
- [Python 爬虫之路4] 使用selenium爬取知乎任意一个问题下,所有回答中的图片
- Python selenium爬虫
- J2EE开发技术点6:Proxool数据库连接池
- maven
- eclipse中将一个项目作为library导入另一个项目中
- 利用BIOS INT 0x13读取软盘
- C++别名传参和指针传参区别
- 爬虫之自动保存文档-使用python/selenium
- 远程连接mysql被拒绝问题
- 23设计模式之代理模式(Proxy)
- 编写留言簿的一个问题, 研究了三天了也没看出来哪里不对, 请各位大神给支个招
- 将代码源文件由utf8转为GBK
- 23设计模式之单例模式(Singleton)
- Photoshop CS6最新官方正式中文破解版(32位、64位)
- An internal error occurred during: "Launching cookie on Tomcat 7.x".
- 23设计模式之迭代器模式(Iterator)