回答編集履歴
3
追記
test
CHANGED
@@ -2,3 +2,57 @@
|
|
2
2
|
サイトを見るとclass='mtx'がは3つしかないと思うのですが、4つ目以降を取得しようとしていることが原因ではないでしょうか?
|
3
3
|
|
4
4
|
参考サイトでは実際のデータもmtxに入っていますが、アップデートされてmtx2に入っているためクラスの指定をmtx2に変更する必要があると思います。
|
5
|
+
|
6
|
+
```python
|
7
|
+
place_name = ["札幌", "秋田", "新潟", "東京", "大阪", "広島", "高知", "熊本", "那覇"]
|
8
|
+
|
9
|
+
place_codeA = [14, 32, 54, 44, 62, 67, 74, 86, 91]
|
10
|
+
|
11
|
+
place_codeB = [47412, 47582, 47604, 47662, 47772, 47765, 47893, 47819, 47936]
|
12
|
+
|
13
|
+
import requests
|
14
|
+
from bs4 import BeautifulSoup
|
15
|
+
import csv
|
16
|
+
|
17
|
+
base_url = "http://www.data.jma.go.jp/obd/stats/etrn/view/annually_s.php?prec_no=%s&block_no=%s&year=2021&month=1&day=1&view=p1"
|
18
|
+
|
19
|
+
def str2float(str):
|
20
|
+
try:
|
21
|
+
return float(str)
|
22
|
+
except:
|
23
|
+
return 0.0
|
24
|
+
|
25
|
+
if __name__ == "__main__":
|
26
|
+
|
27
|
+
for place in place_name:
|
28
|
+
All_list = [['年', '陸の平均気圧(hPa)', '海の平均気圧(hPa)', '降水量(mm)', '平均気温(℃)','最高気温(℃)', '最低気温(℃)', '平均湿度(%)']]
|
29
|
+
print(place)
|
30
|
+
index = place_name.index(place)
|
31
|
+
|
32
|
+
r = requests.get(base_url%(place_codeA[index], place_codeB[index]))
|
33
|
+
r.encoding = r.apparent_encoding
|
34
|
+
|
35
|
+
soup = BeautifulSoup(r.text)
|
36
|
+
|
37
|
+
rows = soup.findAll('tr', class_='mtx2')
|
38
|
+
print(len(rows))
|
39
|
+
# rows = rows[3:]
|
40
|
+
|
41
|
+
for row in rows:
|
42
|
+
data = row.findAll('td')
|
43
|
+
rowData = []
|
44
|
+
rowData.append(str(data[0].string))
|
45
|
+
rowData.append(str2float(data[1].string))
|
46
|
+
rowData.append(str2float(data[2].string))
|
47
|
+
rowData.append(str2float(data[3].string))
|
48
|
+
rowData.append(str2float(data[7].string))
|
49
|
+
rowData.append(str2float(data[10].string))
|
50
|
+
rowData.append(str2float(data[11].string))
|
51
|
+
rowData.append(str2float(data[12].string))
|
52
|
+
All_list.append(rowData)
|
53
|
+
|
54
|
+
with open(place + '.csv', 'w') as file:
|
55
|
+
writer = csv.writer(file, lineterminator='\n')
|
56
|
+
writer.writerows(All_list)
|
57
|
+
|
58
|
+
```
|
2
修正
test
CHANGED
@@ -1,3 +1,4 @@
|
|
1
1
|
`rows = rows[3:]`
|
2
2
|
サイトを見るとclass='mtx'がは3つしかないと思うのですが、4つ目以降を取得しようとしていることが原因ではないでしょうか?
|
3
3
|
|
4
|
+
参考サイトでは実際のデータもmtxに入っていますが、アップデートされてmtx2に入っているためクラスの指定をmtx2に変更する必要があると思います。
|
1
test
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
`rows = rows[3:]`
|
2
|
-
サイトを見るとclass='mtx'がは3つしかないと思うのですが、
|
2
|
+
サイトを見るとclass='mtx'がは3つしかないと思うのですが、4つ目以降を取得しようとしていることが原因ではないでしょうか?
|
3
3
|
|