回答編集履歴
1
追記
test
CHANGED
@@ -15,3 +15,83 @@
|
|
15
15
|
star = float(i['class'][0][5:].replace('_', '.')) # => 4.0
|
16
16
|
|
17
17
|
```
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
----
|
22
|
+
|
23
|
+
(追記)
|
24
|
+
|
25
|
+
[子結合子](https://developer.mozilla.org/ja/docs/Web/CSS/Child_combinator)で探索して、↑で抽出してはどうでしょうか。
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
```python
|
30
|
+
|
31
|
+
from bs4 import BeautifulSoup
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
# 質問とはちょっと違います
|
36
|
+
|
37
|
+
html = """
|
38
|
+
|
39
|
+
<main>
|
40
|
+
|
41
|
+
<li class="review">
|
42
|
+
|
43
|
+
<p class="hyoka">
|
44
|
+
|
45
|
+
<i class="star_4_0">評価</i>
|
46
|
+
|
47
|
+
</p>
|
48
|
+
|
49
|
+
</li>
|
50
|
+
|
51
|
+
<li class="review">
|
52
|
+
|
53
|
+
<p class="hyoka">
|
54
|
+
|
55
|
+
<i class="star_3_0">評価</i>
|
56
|
+
|
57
|
+
</p>
|
58
|
+
|
59
|
+
</li>
|
60
|
+
|
61
|
+
<li class="review">
|
62
|
+
|
63
|
+
<p class="hyoka">
|
64
|
+
|
65
|
+
<i class="star_2_0">評価</i>
|
66
|
+
|
67
|
+
</p>
|
68
|
+
|
69
|
+
</li>
|
70
|
+
|
71
|
+
</main>"""
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
soup = BeautifulSoup(html, 'lxml')
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
for index, e in enumerate(soup.select('p.hyoka > i'), start=1):
|
80
|
+
|
81
|
+
print(float(e['class'][0][5:].replace('_', '.')))
|
82
|
+
|
83
|
+
```
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
結果
|
88
|
+
|
89
|
+
```plain
|
90
|
+
|
91
|
+
4.0
|
92
|
+
|
93
|
+
3.0
|
94
|
+
|
95
|
+
2.0
|
96
|
+
|
97
|
+
```
|