Я пытаюсь загрузить открытые немецкие короткие позиции в формате CSV, но у меня короткая позиция.
Адрес веб-сайта
Мой фиктивный код выглядит следующим образом:
- Загрузите первую страницу и получите файл session.sessionid.
- Используйте идентификатор для перехода по ссылке "Дополнительные параметры поиска"
- Разместите запрос на странице "Дополнительные параметры поиска".
- Получите CSV
Любые советы здесь? Я думаю, у меня проблемы с печеньем. Вот мой код:
<?php
$ch = curl_init('https://www.bundesanzeiger.de/ebanzwww/wexsservlet?global_data.language=en&page.navid=to_nlp_start&session.sessionid=&global_data.designmode=eb');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// get headers too with this line
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0");
//curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__). '/cookie.txt');
$result = curl_exec($ch);
$trueposition = strpos($result, 'session.sessionid=');
echo '<br>--------------------------------------<br>';
echo substr($result,$trueposition+18,32);
$id = substr($result,$trueposition+18,32);
echo '<br>';
echo '-----------------------------------------<br>';
$url = 'https://www.bundesanzeiger.de/ebanzwww/wexsservlet?page.navid=nlpstarttonlpstart_new&nlp_search_param.extended_search=true&session.sessionid=' . $id;
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
$data = array(
"session.sessionid:" => $id,
"nlp_search_param.publisher:" => "",
"nlp_search_param.emittent:" => "",
"nlp_search_param.isin:" => "",
"nlp_search_param.search_history:" => "true",
"nlp_search_param.date_start:0:" => "1",
"nlp_search_param.date_start:1:" => "1",
"nlp_search_param.date_start:2:" => "2001",
"nlp_search_param.date_end:0:" => "1",
"nlp_search_param.date_end:1:" => "1",
"nlp_search_param.date_end:2:" => "2019",
"nlp_search_param.position_start:" => "",
"nlp_search_param.position_end:" => "",
"(page.navid=nlpresultlisttonlpresultlist_updatefilter):" => "Show net short positions"
);
$url = 'https://www.bundesanzeiger.de/ebanzwww/wexsservlet?session.sessionid=' . $id . '&page.navid=nlpresultlisttonlpresultlistdownloadcsv';
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
$url = 'https://www.bundesanzeiger.de/ebanzwww/wexsservlet';
$postvars = http_build_query($data) . "\n";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
echo 'Result:<br>';
var_dump($server_output);
curl_close ($ch);
?>
Обновление: я пытался использовать пример, на который ссылается Найджел, но все же я не могу это сделать. Однако последняя ссылка, которую создает следующий код ($ url), при вставке в мой браузер (chrome) иногда будет работать (csv downloads). Тем не менее, он новее работает с curl.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.bundesanzeiger.de/ebanzwww/wexsservlet?global_data.language=en&page.navid=to_nlp_start&session.sessionid=&global_data.designmode=eb');
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookie.txt'); //could be empty, but cause problems on some hosts
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookie.txt'); //could be empty, but cause problems on some hosts
$answer = curl_exec($ch);
if (curl_error($ch)) {
echo curl_error($ch);
}
$result = $answer;
//var_dump($result);
$trueposition = strpos($result, 'session.sessionid=');
echo '<br>-------------------1-------------------<br>';
echo substr($result,$trueposition+18,32);
$id = substr($result,$trueposition+18,32);
echo '<br>';
echo '-----------------------1------------------<br>';
$url = 'https://www.bundesanzeiger.de/ebanzwww/wexsservlet?page.navid=nlpstarttonlpstart_new&nlp_search_param.extended_search=true&session.sessionid=' . $id;
curl_setopt($ch, CURLOPT_URL, $url);
$answer = curl_exec($ch);
if (curl_error($ch)) {
echo curl_error($ch);
}
$result = $answer;
//var_dump($result);
$trueposition = strpos($result, 'session.sessionid=');
echo '<br>----------------2----------------------<br>';
echo substr($result,$trueposition+18,32);
$id = substr($result,$trueposition+18,32);
echo '<br>';
echo '-------------------2----------------------<br>';
$data = array(
"session.sessionid:" => $id,
"nlp_search_param.publisher:" => "",
"nlp_search_param.emittent:" => "",
"nlp_search_param.isin:" => "",
"nlp_search_param.search_history:" => "false",
"nlp_search_param.date_start:0:" => "1",
"nlp_search_param.date_start:1:" => "1",
"nlp_search_param.date_start:2:" => "2001",
"nlp_search_param.date_end:0:" => "1",
"nlp_search_param.date_end:1:" => "1",
"nlp_search_param.date_end:2:" => "2019",
"nlp_search_param.position_start:" => "",
"nlp_search_param.position_end:" => "",
"(page.navid=nlpresultlisttonlpresultlist_updatefilter):" => "Show net short positions"
);
var_dump($data);
$url = 'https://bundesanzeiger.de/ebanzwww/wexsservlet/';
$postvars = http_build_query($data) . "\n";
//another request preserving the session
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
$answer = curl_exec($ch);
if (curl_error($ch)) {
echo curl_error($ch);
}
$result = $answer;
//var_dump($result);
$trueposition = strpos($result, 'session.sessionid=');
echo '<br>----------------3----------------------<br>';
echo substr($result,$trueposition+18,32);
$id = substr($result,$trueposition+18,32);
echo '<br>';
echo '-------------------3----------------------<br>';
$url = "https://bundesanzeiger.de/ebanzwww/wexsservlet?session.sessionid=" . $id . '&page.navid=nlpresultlisttonlpresultlistdownloadcsv';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
$answer = curl_exec($ch);
if (curl_error($ch)) {
echo curl_error($ch);
}
echo '<br>';
echo '-------------------4----------------------<br>';
echo $url . '<br>';
var_dump($answer);
file_put_contents('test.csv', $answer);