Java 如何从需要按键的URL中删除

Java 如何从需要按键的URL中删除,java,bash,wget,Java,Bash,Wget,我正在尝试从此URL下载: 使用bashwget 但是,我需要手动按下“在此下载历史数据”右侧的链接 在命令行的代码中是否有这样做的方法 EDIT 1 或者来自java也很好。我认为您需要编写一些代码来实现这一点,使用支持Javascript的html客户端库,如的答案中提到的PhantomJS 其他选项包括Python的mechanize库,以及中提到的一些东西 如果您正在Java中寻找一个无头浏览库,我会看看。 不过,我没有亲自使用过它,因此我不能保证它的稳定性、易用性或使用性。您不能下

我正在尝试从此URL下载:

使用bash
wget

但是,我需要手动按下“在此下载历史数据”右侧的链接

在命令行的代码中是否有这样做的方法

EDIT 1

或者来自java也很好。

我认为您需要编写一些代码来实现这一点,使用支持Javascript的html客户端库,如的答案中提到的
PhantomJS

其他选项包括Python的
mechanize
库,以及中提到的一些东西

如果您正在
Java
中寻找一个无头浏览库,我会看看。
不过,我没有亲自使用过它,因此我不能保证它的稳定性、易用性或使用性。

您不能下载它,因为下载是通过JavaScript触发的。
最好是在普通计算机上下载,而不是上传到另一台服务器,通过HTTP直接访问文件。您可以在命令行中下载它。

因为我想自己学习
PhantomJS
,所以我尝试过,但似乎
PhantomJS
还不够成熟,无法正确支持这一点。 因为我花了时间来理解链接是如何工作的,这里有一个
php
中的解决方案,您应该能够复制并粘贴到
Download.php
中,并从命令行运行,假设您安装了
php cli
。我希望它也能作为未来尝试编写此类脚本的人的一个示例

<?php

/**
  * Usage: php Download.php <URL> <FileName>
  * Example: 
  * php Download.php http://www.histdata.com/download-free-forex-historical-data/?/ascii/1-minute-bar-quotes/eurusd/2014/2 Output.zip
  */

// Configuration parameters
$post_url = 'http://www.histdata.com/get.php';
$init_url = $argv[1];
$filename = $argv[2];

$ch = curl_init ($init_url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);

$output = curl_exec ($ch);

// Pull out the cookies
preg_match('/^Set-Cookie:\s*([^;]*)/mi', $output, $m);
parse_str($m[1], $cookies);

// Get the POST parameters from the form.
$post_array = getPostArray($output);
$post_data = http_build_query($post_array);

$header = array();
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: ";
$header[] = "Content-Type: application/x-www-form-urlencoded";

$ch = curl_init ($post_url);
curl_setopt ($ch, CURLOPT_COOKIE, http_build_query($cookies)); 
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); 
curl_setopt($ch, CURLOPT_REFERER, 'http://www.histdata.com/download-free-forex-historical-data/?/ascii/1-minute-bar-quotes/eurusd/2014/2/HISTDATA_COM_ASCII_EURUSD_M1_201402.zip'); 

$output = curl_exec ($ch);
$fp = fopen($filename,'wb') or die('Cannot open file for writing!'. $filename);
fwrite($fp, $output);
fclose($fp);

function getPostArray($doc) {
    $dom_doc = new DOMDocument;
    if (! @$dom_doc->loadhtml($doc))
    {
        die('Could not load html!');
    }
    else
    {
        $xpath = new DOMXpath($dom_doc);

        foreach($xpath->query('//form[@name="file_down"]//input') as $input)
        {
            //get name and value of input
            $input_name = $input->getAttribute('name');
            $input_value = $input->getAttribute('value');
            $post_items[$input_name] = $input_value;
        }
        return $post_items;
    }
}
?>
loadhtml($doc))
{
die('无法加载html!');
}
其他的
{
$xpath=newdomxpath($dom\u doc);
foreach($xpath->query('//form[@name=“file\u down”]///input”)作为$input)
{
//获取输入的名称和值
$input_name=$input->getAttribute('name');
$input_value=$input->getAttribute('value');
$post_项[$input_名称]=$input_值;
}
返回$post_项目;
}
}
?>

有没有一种用java实现的方法?这对我也适用。我正在尝试自动化这个过程。“下载到你的普通电脑”是什么意思?你是说手工做吗?是的,我是说手工。如果您想自动化这个过程,您应该查看浏览器发送的HTTP请求。也许你可以发送更多的请求。为了分析HTTP请求,我推荐Burproxy。哇,对我来说看起来有点技术性!有一个更简单的产品吗?如果这是给你的技术,我很抱歉地说,我的方式得到这个问题的解决是复杂的为你。