質問編集履歴
1
コードの不要部分の修正、格納する変数をoffice_detailにしました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,11 +2,9 @@
|
|
2
2
|
以下のコードですと一つの配列に複数のハッシュが入っている状況です。
|
3
3
|
これを一つの配列につき一つのハッシュにしたいのですが解決方法がわからないので詳しい方いらっしゃいましたらご教示ください。
|
4
4
|
|
5
|
+
*新たに格納する変数はoffice_detailです。
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
```ruby
|
10
8
|
require "net/http"
|
11
9
|
require "rexml/document"
|
12
10
|
|
@@ -17,30 +15,23 @@
|
|
17
15
|
headers = { "Content-Type" => "text/html;charset=utf-8" }
|
18
16
|
response = http.get(uri.path, headers)
|
19
17
|
|
20
|
-
|
21
|
-
|
22
18
|
response.code # status code
|
23
19
|
body = response.body # response body
|
24
20
|
text = body.force_encoding("UTF-8")
|
25
21
|
p text.encoding
|
26
|
-
#p text
|
27
22
|
|
23
|
+
|
28
24
|
array_office = []
|
29
25
|
|
30
|
-
#tables = text.match(/<table(?: .+?)?>.*?</table>/)
|
31
26
|
tables = text.scan(/<table.*?</table>/)
|
32
|
-
#p tables.length
|
33
|
-
#p tables
|
34
27
|
|
35
28
|
|
36
29
|
tables.each do |tbl|
|
37
|
-
# tbl
|
38
30
|
ths = tbl.scan(/<th.*?</th>/)
|
39
|
-
|
31
|
+
ths.length
|
40
32
|
|
41
33
|
tds = tbl.scan(/<td.*?</td>/)
|
42
|
-
|
34
|
+
|
43
|
-
#p tds
|
44
35
|
|
45
36
|
office = {}
|
46
37
|
for num in 1..(ths.length) do
|
@@ -49,12 +40,12 @@
|
|
49
40
|
office.store(title, value)
|
50
41
|
end
|
51
42
|
|
52
|
-
|
43
|
+
|
53
44
|
array_office.push(office)
|
54
45
|
|
55
46
|
end
|
47
|
+
```
|
56
48
|
|
57
|
-
p array_office
|
58
49
|
-----------------------------------
|
59
50
|
現在の出力結果
|
60
51
|
|