forked from ronnywang/realprice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.php
225 lines (205 loc) · 7.6 KB
/
crawler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
class RealPriceCrawler
{
public $city = array(
'A' => '臺北市',
'C' => '基隆市',
'F' => '新北市',
'H' => '桃園縣',
'O' => '新竹市',
'J' => '新竹縣',
'K' => '苗栗縣',
'B' => '臺中市',
'M' => '南投縣',
'N' => '彰化縣',
'P' => '雲林縣',
'I' => '嘉義市',
'Q' => '嘉義縣',
'D' => '臺南市',
'E' => '高雄市',
'T' => '屏東縣',
'G' => '宜蘭縣',
'U' => '花蓮縣',
'V' => '臺東縣',
'X' => '澎湖縣',
'W' => '金門縣',
'Z' => '連江縣',
);
public function getAreasFromCity($city)
{
$url = 'http://lvr.land.moi.gov.tw/N11/pro/setArea.jsp';
$response = http_get($url . '?city=' . urlencode($city), array(
'cookies' => array('JSESSIONID' => $this->cookie)
));
$content = http_parse_message($response)->body;
$country = new SimpleXMLElement($content);
$ret = array();
foreach ($country->Code as $code) {
$ret[strval($code->id)] = strval($code->name);
}
return $ret;
}
public function authCode()
{
// 1. 先輸入認證碼
$url = 'http://lvr.land.moi.gov.tw/N11/ImageNumberN13';
$response = http_get($url);
$message = http_parse_message($response);
if (!preg_match('#JSESSIONID=([^;]*);#', $message->headers['Set-Cookie'], $matches)) {
throw new Exception("找不到 JSESSIONID");
}
$this->cookie = $matches[1];
$tmpfp = tmpfile();
fwrite($tmpfp, $message->body);
$tmp_meta = stream_get_meta_data($tmpfp);
system('jp2a ' . escapeshellarg($tmp_meta['uri']));
fclose($tmpfp);
$code = readline("請輸入認證碼: ");
// 2. 輸入認證碼
$response = http_post_fields('http://lvr.land.moi.gov.tw/N11/login.action', array(
'command' => 'login',
'count' => 0,
'rand_code' => intval($code),
'in_type' => 'land',
), array(), array(
'cookies' => array('JSESSIONID' => $this->cookie),
));
if (strpos(http_parse_message($response)->body, '驗證碼錯誤')) {
error_log('驗證碼錯誤,重試一次');
$this->authCode();
}
}
protected function getBaseOptions($city_id, $area)
{
return array(
'type' => 'Qrydata',
'Qry_city' => $city_id,
'Qry_area_office' => $area,
'Qry_paytype' => '1,2,3,4',
'Qry_build' => '',
'Qry_price_s' => '',
'Qry_price_e' => '',
'Qry_p_yyy_s' => 101,
'Qry_p_yyy_e' => 101,
'Qry_season_s' => '',
'Qry_season_e' => '',
'Qry_doorno' => '',
'Qry_area_s' => '',
'Qry_area_e' => '',
'Qry_order' => 'QA08&desc',
'Qry_unit' => '2',
'Qry_area_srh' => '',
);
}
protected function parseHTML($body)
{
if (strpos($body, '<div class="description">未找到相關資料</div>')) {
return array();
}
$doc = new DOMDocument();
$full_body = '<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></html><body>' . $body . '</body></html>';
@$doc->loadHTML($full_body);
$result_dom = $doc->getElementById('hiddenresult');
if (is_null($result_dom)) {
var_dump($body);
throw new Exception('找不到 div#hiddenresult');
}
$count = 0;
$results = array();
foreach ($result_dom->childNodes as $div_result_dom) {
if ('#text' == $div_result_dom->nodeName) {
continue;
}
if ('div' != $div_result_dom->nodeName) {
throw new Exception("div#hiddenresult 下面只會是 div.result");
}
foreach ($div_result_dom->childNodes as $tr_dom) {
if ('tr' == $tr_dom->nodeName) {
$count ++;
$results[$count] = new StdClass;
$results[$count]->content = $doc->saveHTML($tr_dom);
} elseif ('script' == $tr_dom->nodeName) {
$results[$count]->script = $doc->saveHTML($tr_dom);
}
}
}
return array_values($results);
}
protected $_last_fetch = null;
protected function getBodyFromOptions($options)
{
$post_data = array();
foreach ($options as $key => $value) {
$post_data[$key] = base64_encode($value);
}
while (!is_null($this->_last_fetch) and (microtime(true) - $this->_last_fetch) < 3) {
sleep(1);
}
$this->_last_fetch = microtime(true);
$url = 'http://lvr.land.moi.gov.tw/N11/LandBuildBiz';
$response = http_post_fields($url, $post_data, array(), array(
'cookies' => array('JSESSIONID' => $this->cookie),
'useragent' => 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4',
'referer' => 'http://lvr.land.moi.gov.tw/N11/login.action',
'headers' => array(
'Content-Type' => 'application/x-www-form-urlencoded',
'Origin' => 'http://lvr.land.moi.gov.tw',
'X-Requested-With' => 'XMLHttpRequest',
),
));
$message = http_parse_message($response);
if (strpos($message->body, '系統連線已逾時,請重新登入')) {
$this->authCode();
return $this->getBodyFromOptions($options);
}
if ('' == $message->body) {
var_dump($response);
print_r($options);
throw new Exception("抓到的內容是空的");
}
return $message->body;
}
public function crawlerData($city_id, $area)
{
error_log($area);
$options = $this->getBaseOptions($city_id, $area);
$datas = array();
$last_price = null;
while (true) {
$body = $this->getBodyFromOptions($options);
$results = $this->parseHTML($body);
if (count($results) < 28) {
break;
}
$datas = array_merge($datas, $results);
if (!preg_match('#<td class="text_10">交易總價:</td>\s+<td width="34%" class="text_red_bold">([^<]*)</td>#m', $results[count($results) - 1]->content, $matches)) {
throw new Exception('找不到任何價錢?');
}
$price = intval(str_replace(',', '', $matches[1]));
if ($last_price == $price) {
file_put_contents(
__DIR__ . '/warnings',
"{$area} 價錢為 {$price} 超過 28 筆,可能會遺漏資料\n",
FILE_APPEND
);
break;
}
$last_price = $price;
$options['Qry_price_e'] = intval($price / 10000);
echo $price . "\n";
}
return $datas;
}
}
$crawler = new RealPriceCrawler;
$crawler->authCode();
foreach ($crawler->city as $city_id => $city_name) {
foreach ($crawler->getAreasFromCity($city_id) as $area_id => $area_name) {
$filename = $area_id . '-' . $city_name . '-' . $area_name;
if (file_exists(__DIR__ . '/raw/' . $filename)) {
continue;
}
$data = $crawler->crawlerData($city_id, $area_id);
file_put_contents(__DIR__ . '/raw/' . $filename, json_encode($data));
}
}