質問編集履歴
2
実行結果を載せました
test
CHANGED
File without changes
|
test
CHANGED
@@ -191,3 +191,23 @@
|
|
191
191
|
どなたかご回答いただけると幸いです。
|
192
192
|
|
193
193
|
よろしくお願いいたします。
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
文字化けを追記いたします。
|
200
|
+
|
201
|
+
```
|
202
|
+
|
203
|
+
[[u'100\u5747\u306e\u7b4b\u30c8\u30ec\u30b0\u30c3\u30ba\u304a\u3059\u3059\u3081TOP6\uff01\u81ea\u5b85\u30c8\u30ec\u30fc\u30cb\u30f3\u30b0\u3092\u5145\u5b9f\u3055\u305b\u3088\u3046', 'https://kintore.site/14354'], [u'\u7b4b\u30c8\u30ec\u3092\u3059\u308b\u3068\u30cf\u30b2\u308b!?\u7b4b\u30c8\u30ec\u3068\u629c\u3051\u6bdb\u306e\u95a2\u4fc2\u6027\u306e\u30a6\u30bd\u30fb\u30db\u30f3\u30c8', 'https://kintore.site/14376'], [u'\u5927\u81c0\u7b4b\u306e\u304a\u3059\u3059\u3081\u30c8\u30ec\u30fc\u30cb\u30f3\u30b0\uff15\u9078\uff01\u52b9\u679c\u7684\u306a\u935b\u3048\u65b9\u306b\u3064\u3044\u3066\u89e3\u8aac', 'https://kintore.site/14347'], [u'\u7b4b\u30c8\u30ec\u306b\u30de\u30a6\u30b9\u30d4\u30fc\u30b9\uff1f\u305d\u306e\u610f\u5916\u306a\u52b9\u679c\u3068\u5931\u6557\u3057\u306a\u3044\u305f\u3081\u306e\u9078\u3073\u65b9', 'https://kintore.site/14265'], [u'\u4e0b\u534a\u8eab\u75e9\u305b\u306b\u52b9\u304f\u98df\u4e8b\u30e1\u30cb\u30e5\u30fc\u3002\u4e0b\u534a\u8eab\u592a\u308a\u306e\u539f\u56e0\u306f\u30b3\u30ec\u3060\u3063\u305f\uff01', 'https://kintore.site/14263'], [u'\u7b4b\u30c8\u30ec\u306e\u547c\u5438\u6cd5\u3092\u7a2e\u76ee\u5225\u306b\u7d39\u4ecb\uff01\u610f\u8b58\u3059\u308b\u3060\u3051\u3067\u7b4b\u30c8\u30ec\u52b9\u679c\u304c\u5927\u5e45\u30a2\u30c3\u30d7', 'https://kintore.site/14531']]
|
204
|
+
|
205
|
+
```
|
206
|
+
|
207
|
+
2回目を動かすと更新情報がないので下記のように表示されるのですが文字化けしていません。
|
208
|
+
|
209
|
+
```
|
210
|
+
|
211
|
+
[['更新情報はありませんhttps://kintore.site']]
|
212
|
+
|
213
|
+
```
|
1
参考コードを載せました
test
CHANGED
File without changes
|
test
CHANGED
@@ -4,7 +4,161 @@
|
|
4
4
|
|
5
5
|
https://qiita.com/taka-kawa/items/f0597b2f375da7ddbb73
|
6
6
|
|
7
|
+
```
|
7
8
|
|
9
|
+
import urllib.request as url_req
|
10
|
+
|
11
|
+
from bs4 import BeautifulSoup
|
12
|
+
|
13
|
+
from config import config, update_recent_article
|
14
|
+
|
15
|
+
from slack_notification import slack_notify
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
first_view = url_req.urlopen(config['web_info']['url']).read()
|
20
|
+
|
21
|
+
soup = BeautifulSoup(first_view, "lxml")
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
def extract_pick_up(soup=soup):
|
26
|
+
|
27
|
+
"""
|
28
|
+
|
29
|
+
指定されたページのhtmlを読み込み、最新記事を抜粋してくる
|
30
|
+
|
31
|
+
"""
|
32
|
+
|
33
|
+
columns = soup.find_all("article", class_="post-list-item")
|
34
|
+
|
35
|
+
return columns[0:13]
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
def extract_url(column):
|
42
|
+
|
43
|
+
"""
|
44
|
+
|
45
|
+
columns @params: コラム
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
コラムのurlを取得する
|
50
|
+
|
51
|
+
"""
|
52
|
+
|
53
|
+
column_html = BeautifulSoup(str(column), "lxml")
|
54
|
+
|
55
|
+
url = column_html.find("a").get("href")
|
56
|
+
|
57
|
+
return url
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
def extract_title(column):
|
64
|
+
|
65
|
+
"""
|
66
|
+
|
67
|
+
columns @params: コラム
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
コラムのタイトルを取得する
|
72
|
+
|
73
|
+
"""
|
74
|
+
|
75
|
+
column_html = BeautifulSoup(str(column), "lxml")
|
76
|
+
|
77
|
+
title = column_html.find("h2").string
|
78
|
+
|
79
|
+
return title
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
def extract_update_article(columns):
|
84
|
+
|
85
|
+
"""
|
86
|
+
|
87
|
+
更新された記事を抽出
|
88
|
+
|
89
|
+
更新前の最新の記事のindexはconfig.iniで管理
|
90
|
+
|
91
|
+
"""
|
92
|
+
|
93
|
+
# 最新の記事番号を取得
|
94
|
+
|
95
|
+
recent_article = config['web_info']['recent_article']
|
96
|
+
|
97
|
+
# 更新された記事のurlを取得
|
98
|
+
|
99
|
+
article_list = []
|
100
|
+
|
101
|
+
update_start = False
|
102
|
+
|
103
|
+
# 逆順に取得していき、最新記事の次からの記事のurlを取得
|
104
|
+
|
105
|
+
for column in reversed(columns):
|
106
|
+
|
107
|
+
url = extract_url(column)
|
108
|
+
|
109
|
+
# 記事番号取得
|
110
|
+
|
111
|
+
article_num = url.replace(config['web_info']['url']+"/", "")
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
# タイトル取得
|
116
|
+
|
117
|
+
title = extract_title(column)
|
118
|
+
|
119
|
+
# 取得すべきurl
|
120
|
+
|
121
|
+
if update_start:
|
122
|
+
|
123
|
+
article_list.append([title, url])
|
124
|
+
|
125
|
+
recent_article = article_num
|
126
|
+
|
127
|
+
continue
|
128
|
+
|
129
|
+
# 前回取得した最新の記事かどうか判定
|
130
|
+
|
131
|
+
if recent_article == article_num:
|
132
|
+
|
133
|
+
update_start = True
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
# config更新
|
138
|
+
|
139
|
+
update_recent_article(recent_article)
|
140
|
+
|
141
|
+
if article_list == []:
|
142
|
+
|
143
|
+
return [["更新記事はありません", "https://kintore.site"]]
|
144
|
+
|
145
|
+
return article_list
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
if __name__ == "__main__":
|
152
|
+
|
153
|
+
columns = extract_pick_up()
|
154
|
+
|
155
|
+
url = extract_url(columns[0])
|
156
|
+
|
157
|
+
article_list = extract_update_article(columns)
|
158
|
+
|
159
|
+
slack_notify(text_="---プロたんの記事---", list_=article_list)
|
160
|
+
|
161
|
+
```
|
8
162
|
|
9
163
|
前回のスクレイピングで取得していない新着記事があった場合そのタイトルとURLをリストに入れてそれをスラックで通知するプログラムみたいです。
|
10
164
|
|
@@ -20,7 +174,7 @@
|
|
20
174
|
|
21
175
|
article_list = extract_update_article(columns)
|
22
176
|
|
23
|
-
print str(article_list).decode('string-escape')
|
177
|
+
print str(article_list).decode('string-escape')←この行を追記したのですが文字化けします
|
24
178
|
|
25
179
|
```
|
26
180
|
|