質問編集履歴

1

コード追記

2018/11/03 05:42

投稿

yutoooo
yutoooo

スコア13

test CHANGED
File without changes
test CHANGED
@@ -17,3 +17,69 @@
17
17
  <p class="weather-telop">晴</p>
18
18
 
19
19
  ```
20
+
21
+ 追記 
22
+
23
+ ```python
24
+
25
+ import requests
26
+
27
+ from bs4 import BeautifulSoup
28
+
29
+ #目的のURL
30
+
31
+ url = "https://tenki.jp/forecast/3/16/4410/13103/"
32
+
33
+
34
+
35
+ #HTTPリクエスト
36
+
37
+ r = requests.get(url)
38
+
39
+
40
+
41
+ bsObj = BeautifulSoup(r.content,"html.parser")
42
+
43
+
44
+
45
+ today = bsObj.find(class_="today-weather")
46
+
47
+ weather = today.p.string
48
+
49
+
50
+
51
+ #気温情報
52
+
53
+ temp = today.div.find(class_="date-value-wrap")
54
+
55
+ #<dd>タグを全て取り出す
56
+
57
+ temp=temp.find_all("dd")
58
+
59
+ #最高気温
60
+
61
+ temp_max = temp[0].span.string
62
+
63
+ #最高気温の前日比
64
+
65
+ temp_max_diff = temp[1].string
66
+
67
+ #最低気温
68
+
69
+ temp_min = temp[2].span.string
70
+
71
+ #最低気温の前日比
72
+
73
+ temp_min_diff = temp[3].string
74
+
75
+
76
+
77
+ print("天気:{}".format(weather))
78
+
79
+ print("最高気温:{}{}".format(temp_max,temp_max_diff))
80
+
81
+ print("最低気温:{}{}".format(temp_min,temp_min_diff))
82
+
83
+
84
+
85
+ ```