teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

nextタグがない場合の処理追加

2019/06/24 14:17

投稿

barobaro
barobaro

スコア1286

answer CHANGED
@@ -4,17 +4,20 @@
4
4
 
5
5
  ```python
6
6
  for i in soup.find_all("a"):
7
-
7
+
8
8
  result = []
9
9
 
10
10
  result.append(i.get_text())
11
11
 
12
12
  j = i.find_next()
13
13
 
14
+ if j:
14
- if j.name == "p" and j.get("class") == ["profileName"]:
15
+ if j.name == "p" and j.get("class") == ["profileName"]:
15
- result.append(j.get_text())
16
+ result.append(j.get_text())
17
+ else:
18
+ result.append(None)
16
19
  else:
17
20
  result.append(None)
18
-
21
+
19
22
  print(result)
20
23
  ```