回答編集履歴

1

a

2018/10/16 11:46

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -38,43 +38,53 @@
38
38
 
39
39
  soup = BeautifulSoup(req.read(), 'html.parser')
40
40
 
41
-
42
-
43
- poem_elem = soup.select('td[height=40] b')
44
-
45
- if not poem_elem:
46
-
47
- return False # 存在しないID
48
-
49
41
 
50
42
 
51
43
  # 俳句
52
44
 
53
- poem_elem = poem_elem[0]
45
+ poem_elem = soup.select('td[height=40] b')[0]
54
46
 
55
47
  poem = poem_elem.text.replace('*', '').strip() # サニタイズ
56
48
 
49
+ if not poem:
50
+
51
+ return False # 存在しないID
52
+
57
53
  # 作者
58
54
 
59
- author_elem = soup.select('table[width=85%] tr:nth-of-type(1) td:nth-of-type(2)')[0]
55
+ author_elem = soup.select('table[cellspacing="1"] tr:nth-of-type(1) td:nth-of-type(2)')[0]
60
56
 
61
57
  author = author_elem.text.strip() # サニタイズ
62
58
 
63
59
  # 季語
64
60
 
65
- season_word_elem = soup.select('table[width=85%] tr:nth-of-type(2) td:nth-of-type(2)')[0]
61
+ season_word_elem = soup.select('table[cellspacing="1"] tr:nth-of-type(2) td:nth-of-type(2)')[0]
66
62
 
67
63
  season_word = season_word_elem.text.strip() # サニタイズ
68
64
 
69
65
  # 季節
70
66
 
71
- season_elem = soup.select('table[width=85%] tr:nth-of-type(3) td:nth-of-type(2)')[0]
67
+ season_elem = soup.select('table[cellspacing="1"] tr:nth-of-type(3) td:nth-of-type(2)')[0]
72
68
 
73
69
  season = season_elem.text.strip() # サニタイズ
74
70
 
75
-
71
+ # 出典
76
72
 
73
+ source_elem = soup.select('table[cellspacing="1"] tr:nth-of-type(4) td:nth-of-type(2)')[0]
74
+
75
+ source = source_elem.text.strip() # サニタイズ
76
+
77
+ # 前書
78
+
79
+ foreword_elem = soup.select('table[cellspacing="1"] tr:nth-of-type(5) td:nth-of-type(2)')[0]
80
+
81
+ foreword = foreword_elem.text.strip() # サニタイズ
82
+
83
+
84
+
77
- return {'poem': poem, 'author': author, 'season_word': season_word, 'season': season}
85
+ return {'poem': poem, 'author': author, 'season_word': season_word,
86
+
87
+ 'season': season, 'source': source, 'foreword': foreword}
78
88
 
79
89
  ```
80
90
 
@@ -124,9 +134,7 @@
124
134
 
125
135
  with open('output.csv', 'w', newline='', encoding='utf-8') as f:
126
136
 
127
- fieldnames = ['first_name', 'last_name']
128
-
129
- writer = csv.DictWriter(f, fieldnames=['poem', 'author', 'season_word', 'season'])
137
+ writer = csv.DictWriter(f, fieldnames=['poem', 'author', 'season_word', 'season', 'source', 'foreword'])
130
138
 
131
139
  writer.writeheader()
132
140