【问题标题】:Automate daily csv file download from website button click从网站按钮自动下载每日 csv 文件
【发布时间】:2016-07-02 22:15:07
【问题描述】:

我想自动化访问网站、单击按钮和保存文件的过程。在此站点上下载文件的唯一方法是单击按钮。您无法使用 url 导航到文件。

我一直在尝试使用 phantomjs 和 casperjs 来自动化这个过程,但没有任何成功。

我最近在这里尝试使用布兰登的解决方案 Grab the resource contents in CasperJS or PhantomJS

这是我的代码

var fs = require('fs');
var cache = require('./cache');
var mimetype = require('./mimetype');
var casper = require('casper').create();

casper.start('http://www.example.com/page_with_download_button', function() {

});

casper.then(function() {    
     this.click('#download_button');
 });

 casper.on('resource.received', function (resource) {
     "use strict";
    for(i=0;i < resource.headers.length; i++){
        if(resource.headers[i]["name"] == "Content-Type" && resource.headers[i]["value"] == "text/csv; charset-UTF-8;"){
            cache.includeResource(resource);
        }
    }
 });

 casper.on('load.finished', function(status) {
    for(i=0; i< cache.cachedResources.length; i++){
        var file = cache.cachedResources[i].cacheFileNoPath;
        var ext = mimetype.ext[cache.cachedResources[index].mimetype];
        var finalFile = file.replace("."+cache.cacheExtension,"."+ext);
        fs.write('downloads/'+finalFile,cache.cachedResources[i].getContents(),'b');
    }
});

casper.run();

我认为问题可能是由于我在 cache.js 中的 cachePath 不正确

exports.cachePath = 'C:/Users/username/AppData/Local/Ofi Labs/PhantomJS';

除了反斜杠之外,我还应该使用其他东西来定义路径吗?

当我尝试时

 casperjs --disk-cache=true export_script.js

没有下载任何内容。经过一点调试,我发现cache.cachedResources总是空的。

我也愿意接受 phantomjs/casperjs 之外的解决方案。


更新

我不再尝试使用 CasperJS/PhantomJS 来实现这一点。 我正在使用 dandavis 建议的 chrome 扩展 Tampermonkey。 Tampermonkey 非常容易破解。 我安装了 Tampermonkey,导航到带有下载链接的页面,然后单击 Tampermonkey 下的 New Script 并添加我的 javascript 代码。

document.getElementById("download_button").click();

现在,每次我在浏览器中导航到该页面时,都会下载该文件。然后我创建了一个看起来像这样的批处理脚本

set date=%DATE:~10,4%_%DATE:~4,2%_%DATE:~7,2%
chrome "http://www.example.com/page-with-dl-button"
timeout 10
move "C:\Users\user\Downloads\export.csv" "C:\path\to\dir\export_%date%.csv"

我使用 Windows 任务调度程序将该批处理脚本设置为每晚运行。

成功了!

【问题讨论】:

  • tampermonkey 是一种在访问某个页面时单击按钮的简单方法。 window 的计划任务是按计划打开 url 的好方法:只需运行 url 或指定并运行例如。 chrome.exe 'http://example.com' 什么的。您也可以运行快捷方式文件...无论如何,浏览器会打开该页面,然后 Tampermonkey 单击按钮。一直在很少使用的桌面上使用它。
  • 使用selenium wedriver,看这个链接yizeng.me/2014/05/23/…
  • 谢谢!我从来没有听说过tampermonkey。
  • 你可能应该移动你的事件处理程序之前 casper.start()
  • 反斜杠需要转义! exports.cachePath = 'C:\\Users\\username\\AppData\\Local\\Ofi Labs\\PhantomJS';,不过windows也支持正斜杠exports.cachePath = 'C:/Users/username/AppData/Local/Ofi Labs/PhantomJS';

标签: javascript csv automation phantomjs casperjs


【解决方案1】:

您的按钮很可能会向服务器发出 POST 请求。 为了跟踪它:

  1. Chrome developer tools 中打开网络标签
  2. 导航到页面并点击按钮。
  3. 请注意导致文件下载的请求。右键单击它并复制为 cURL
  4. 运行复制的 cURL

一旦 cURL 正常工作,您可以使用 cron 或任务计划程序来安排下载,具体取决于您使用的操作系统。

【讨论】:

  • 该按钮确实发出了一个 POST 请求。开发工具网络选项卡中的 url 与页面 url 相同。我首先尝试了一个 php curl 解决方案,但我永远无法返回文件。它总是返回一条错误消息。网页使用某种验证来检查按钮是否被实际点击。
  • 您能否用使用纯 HTTP 方法时遇到的错误更新您的问题?可能你只需要添加一个 cookie,就可以放弃 *JS 工具。
猜你喜欢
  • 2022-01-23
  • 2020-11-23
  • 1970-01-01
  • 2017-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-23
  • 2021-10-30
相关资源
最近更新 更多