PHPのWebスクレイピングで、分からない点があって困っています。
以下のコードですが、あるサイトにアクセスして取り出したデータを元に、さらに次のページへアクセスするというものですが、コメント部のSTEP1では、対象のデータを正規表現で取り出すことに成功していますが、
続くSTEP2で、STEP1で抽出したデータを使用しても、
PHP Warning: file_get_contents(http://www.bosch.co.jp/jp/aa/fit_search/step-2.asp): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error
と出てしまい、アクセスすることができないのです。
本当ならば、
http://www.bosch.co.jp/jp/aa/fit_search/**step-2.asp?for_domestic=yes&fit_group=31**
という形でアクセスしたいです。
どなたかこれの解決方法をご教授いただけないでしょうか?
よろしくお願い致します。
PHP
1<?php 2/** 3 * Web Scraping 4 */ 5 6require_once 'simple_html_dom.php'; 7 8class sample { 9 10 public $list = array(); 11 12 /** 13 * __construct 14 */ 15 function __construct() { 16 $this->getList(); 17 } 18 19 /** 20 * 指定したURLにデータとPOSTを送信 21 */ 22 function post($url, $data) { 23 $headers = array('Content-Type: application/x-www-form-urlencoded'); 24 $options = array('http' => array( 25 'method' => 'POST', 26 'content' => http_build_query($data), 27 'header' => implode("\r\n", $headers), 28 )); 29 return file_get_contents($url, false, stream_context_create($options)); 30 } 31 32 /** 33 * 取得する全リストを取得 34 */ 35 function getList() { 36 37 // Step 1 38 $url = 'http://www.bosch.co.jp/jp/aa/fit_search/step-1.asp'; 39 $data = array( 40 'none' => '' 41 ); 42 $html = str_get_html($this->post($url, $data)); 43 $script = $html->find('ul[id=groupList_category_2_domestic]', 0); 44 preg_match_all('/<a href=".\/(.+?)"*>/', $script, $matches_1); 45 46 foreach ($matches_1[1] as $v) { 47 preg_match('/.*=(.+?)&.*/', $v, $for_inout); 48 preg_match('/.*=([1-9]{1}[0-9]{0,1})/', $v, $fit_group); 49 $data = array( 50 'for_domestic' => $for_inout, 51 'fit_group' => $fit_group, 52 ); 53 54 // Step 2 55 $url = 'http://www.bosch.co.jp/jp/aa/fit_search/step-2.asp'; 56 $html = str_get_html($this->post($url, $data)); 57 } 58 59 $html->clear(); 60 unset($html); 61 } 62 63} 64 65$a = new sample();

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/09 04:17