0
私はcurlで検索フォームをスクラップしようとしています(PHP経由)。私はすべてが正しい、またはそれに近いと思ったが、そうは思われない。ちょっとした背景を与えるために、ユーザーが日付範囲を挿入して検索を送信する検索フォームからデータを収集(またはスクレイプ)しようとしています。検索結果の下に結果が表示されます。このページではAJAX/JavaScriptを使用してデータをロードしています。Ajax/Javascriptを使用するWebサイトからのデータの収集。 - CURL
私はPHPスクリプトを実行すると、何も得られません。結果を見るにはprint_r
を追加しましたが、何も表示されません。
ここに私のスクリプトです。すべてのアドバイスは大歓迎です。
<?php
class Scraper {
// Class constructor method
function __construct() {
$this->useragent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3';
$handle = fopen('cookie.txt', 'w') or exit('Unable to create or open cookie.txt file.'."\n"); // Opening or creating cookie file
fclose($handle); // Closing cookie file
$this->cookie = 'cookie.txt'; // Setting a cookie file to store cookie
$this->timeout = 30; // Setting connection timeout in seconds
}
// Method to search and scrape search details
public function scrapePersons($searchString = '') {
$searchUrl = 'https://virre.prh.fi/novus/publishedEntriesSearch';
$postValues = array(
'businessId' => '',
'startDate' => '07072016',
'endDate' => '08072016',
'registrationTypeCode' => 'kltu.U',
'_todayRegistered' => 'on',
'domicileCode' => '091',
'_domicileCode' => '1',
'_eventId_search' => 'Search',
'execution' => 'e2s1',
'_defaultEventId' => '',
);
$search = $this->curlPostFields($searchUrl, $postValues);
return $search;
}
// Method to make a POST request using form fields
public function curlPostFields($postUrl, $postValues) {
$_ch = curl_init(); // Initialising cURL session
// Setting cURL options
curl_setopt($_ch, CURLOPT_SSL_VERIFYPEER, FALSE); // Prevent cURL from verifying SSL certificate
curl_setopt($_ch, CURLOPT_FAILONERROR, TRUE); // Script should fail silently on error
curl_setopt($_ch, CURLOPT_COOKIESESSION, TRUE); // Use cookies
curl_setopt($_ch, CURLOPT_FOLLOWLOCATION, TRUE); // Follow Location: headers
curl_setopt($_ch, CURLOPT_RETURNTRANSFER, TRUE); // Returning transfer as a string
curl_setopt($_ch, CURLOPT_COOKIEFILE, $this->cookie); // Setting cookiefile
curl_setopt($_ch, CURLOPT_COOKIEJAR, $this->cookie); // Setting cookiejar
curl_setopt($_ch, CURLOPT_USERAGENT, $this->useragent); // Setting useragent
curl_setopt($_ch, CURLOPT_URL, $postUrl); // Setting URL to POST to
curl_setopt($_ch, CURLOPT_CONNECTTIMEOUT, $this->timeout); // Connection timeout
curl_setopt($_ch, CURLOPT_TIMEOUT, $this->timeout); // Request timeout
curl_setopt($_ch, CURLOPT_POST, TRUE); // Setting method as POST
curl_setopt($_ch, CURLOPT_POSTFIELDS, $postValues); // Setting POST fields (array)
$results = curl_exec($_ch); // Executing cURL session
curl_close($_ch); // Closing cURL session
return $results;
}
// Class destructor method
function __destruct() {
// Empty
}
}
$testScrape = new Scraper(); // Instantiating new object
$data = json_decode($testScrape->scrapePersons()); // Scraping people records
print_r($data);
?>
返信いただきありがとうございます。あなたは上記のコードを複製することに言及しました。そのコードをPHPスクリプトなどに追加することを意味しますか?私を許してください。私の脳は揚げられている。 – MangoPie
cURL内にもセキュリティチェックを実装する必要があります。したがって、アクション(変更しないでください)、j_username、j_passwordとPOSTを取得する必要があります。最後に2つのPOSTリクエストを行います.1つは検索を実行し、もう1つはセキュリティチェックを通過します。 – pmcilwaine