質問編集履歴
3
文章変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -22,7 +22,7 @@
|
|
22
22
|
|
23
23
|
**●URLの短縮**
|
24
24
|
|
25
|
-
はそれぞれ動くのですが、併せるとエラーが発生します。
|
25
|
+
はそれぞれ動くのですが、併せるとエラーが発生します。それぞれ動くのに併せると動かないということで混乱しています。
|
26
26
|
|
27
27
|
|
28
28
|
|
2
質問文の見易さ変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
こんにちは。プログラミングを勉強し始めたばかりです。
|
2
2
|
|
3
3
|
このサイトの使い方もまだ分かっていません…。
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
環境:MAC(High Sierra), Anaconda, Jupyter note, python3
|
4
8
|
|
5
9
|
|
6
10
|
|
@@ -12,35 +16,63 @@
|
|
12
16
|
|
13
17
|
|
14
18
|
|
15
|
-
Googlenewsのスクレイピング&line通知
|
19
|
+
**●Googlenewsのスクレイピング&line通知**
|
16
20
|
|
17
21
|
と
|
18
22
|
|
19
|
-
URLの短縮
|
23
|
+
**●URLの短縮**
|
20
24
|
|
21
|
-
はそれぞれ動くのですが、併せると
|
25
|
+
はそれぞれ動くのですが、併せるとエラーが発生します。
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
エラーは以下の通りです
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
---------------------------------------------------------------------------
|
34
|
+
|
35
|
+
**TypeError Traceback (most recent call last)**
|
36
|
+
|
37
|
+
<ipython-input-38-670057aa6152> in <module>()
|
38
|
+
|
39
|
+
33 address = "(https://news.google.com"+add+"?hl=en-US&gl=US&ceid=US%3Aen)"
|
40
|
+
|
41
|
+
34
|
42
|
+
|
43
|
+
**--->** 35 urler = get_shortenURL(address)
|
44
|
+
|
45
|
+
36 print(address)
|
46
|
+
|
47
|
+
37 lineNotify("\n●"+title+"\n"+urler)
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
<ipython-input-34-b26a9183e445> in get_shortenURL(longUrl)
|
52
|
+
|
53
|
+
25 'longurl':longUrl
|
54
|
+
|
55
|
+
26 }
|
56
|
+
|
57
|
+
**--->** 27 r = requests.get(url,params=query).json()['data']['url']
|
58
|
+
|
59
|
+
28 return r
|
60
|
+
|
61
|
+
29
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
**TypeError: list indices must be integers or slices, not str**
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
---------------------------------------------------------------------------
|
22
70
|
|
23
71
|
|
24
72
|
|
25
73
|
|
26
74
|
|
27
|
-
◆list indices must be integers or slices, not str
|
28
|
-
|
29
|
-
◯ r = requests.get(url,params=query).json()['data']['url']
|
30
|
-
|
31
|
-
|
75
|
+
```
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
◆Traceback (most recent call last)
|
36
|
-
|
37
|
-
の2つです。
|
38
|
-
|
39
|
-
正直ググれば分かると舐めていたのですが、見つかりません…
|
40
|
-
|
41
|
-
ご協力お願い致します。
|
42
|
-
|
43
|
-
|
44
76
|
|
45
77
|
import requests
|
46
78
|
|
@@ -52,11 +84,9 @@
|
|
52
84
|
|
53
85
|
|
54
86
|
|
55
|
-
|
56
|
-
|
57
87
|
def lineNotify(message):
|
58
88
|
|
59
|
-
line_notify_token = "###
|
89
|
+
line_notify_token = "###LINEのトークン入力"
|
60
90
|
|
61
91
|
line_notify_api = 'https://notify-api.line.me/api/notify'
|
62
92
|
|
@@ -72,7 +102,7 @@
|
|
72
102
|
|
73
103
|
url = 'https://api-ssl.bitly.com/v3/shorten'
|
74
104
|
|
75
|
-
access_token = '###
|
105
|
+
access_token = '###BITLYのトークン入力'
|
76
106
|
|
77
107
|
query = {
|
78
108
|
|
@@ -90,13 +120,13 @@
|
|
90
120
|
|
91
121
|
|
92
122
|
|
93
|
-
res = requests.get(
|
123
|
+
res = requests.get("https://news.google.com/topics/CAAqBwgKMMKU8AowsNG3Ag?hl=en-US&gl=US&ceid=US%3Aen") ###例で今回はFASHIOのカテゴリーからスクレイピングしようとしています。
|
94
124
|
|
95
125
|
res.raise_for_status()
|
96
126
|
|
97
127
|
soup = BeautifulSoup(res.text, "html.parser")
|
98
128
|
|
99
|
-
elems = soup.select("h3 a") ###
|
129
|
+
elems = soup.select("h3 a") ###目的のHTMLコード指しています。
|
100
130
|
|
101
131
|
|
102
132
|
|
@@ -108,12 +138,18 @@
|
|
108
138
|
|
109
139
|
|
110
140
|
|
111
|
-
address = "(https://news.google.com"+ add +"?hl=en-US&gl=US&ceid=US%3Aen)" ###アメリカのGooglenewsから引き抜
|
141
|
+
address = "(https://news.google.com"+ add +"?hl=en-US&gl=US&ceid=US%3Aen)" ###アメリカのGooglenewsから引き抜いて来たURLの足りない部分を付け足しています。
|
112
142
|
|
113
143
|
|
114
144
|
|
115
|
-
urler = get_shortenURL(address)
|
145
|
+
urler = get_shortenURL(address) ###変数名に深い意味はありません
|
116
146
|
|
117
|
-
lineNotify("\n●"+title+"\n"+urler) ### LINEに
|
147
|
+
lineNotify("\n●"+title+"\n"+urler) ### LINEに通知させる本文です。
|
118
148
|
|
119
|
-
time.sleep(60) ###
|
149
|
+
time.sleep(60) ###今の段階では60秒ごとに1記事ずつ通知させるように仮に設定しているだけです。
|
150
|
+
|
151
|
+
```
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
初歩的なミスだとは思うのですが、どうぞよろしくお願いいたします。
|
1
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,49 +1,119 @@
|
|
1
|
+
こんにちは。プログラミングを勉強し始めたばかりです。
|
2
|
+
|
1
|
-
|
3
|
+
このサイトの使い方もまだ分かっていません…。
|
2
4
|
|
3
5
|
|
4
6
|
|
5
|
-
|
7
|
+
早速質問です。
|
6
8
|
|
7
|
-
|
9
|
+
Googlenewsから目的のカテゴリーのnewsを引き抜いてきてlineに通知するものを作ってみたくてネットで拾ったものなどを組み合わせたりしています。(まだまだ初期段階ですので足りないものは沢山あるかと思います。)
|
8
10
|
|
9
|
-
|
11
|
+
問題を解決しながら学習していましたが、早々に引っかかってしまいました。
|
10
12
|
|
11
13
|
|
12
14
|
|
15
|
+
Googlenewsのスクレイピング&line通知
|
16
|
+
|
17
|
+
と
|
18
|
+
|
19
|
+
URLの短縮
|
20
|
+
|
13
|
-
|
21
|
+
はそれぞれ動くのですが、併せるとどうにもエラーが発生します。
|
14
22
|
|
15
23
|
|
16
24
|
|
17
|
-
```
|
18
25
|
|
19
|
-
エラーメッセージ
|
20
26
|
|
27
|
+
◆list indices must be integers or slices, not str
|
28
|
+
|
29
|
+
◯ r = requests.get(url,params=query).json()['data']['url']
|
30
|
+
|
21
|
-
|
31
|
+
の部分です。
|
22
32
|
|
23
33
|
|
24
34
|
|
35
|
+
◆Traceback (most recent call last)
|
36
|
+
|
37
|
+
の2つです。
|
38
|
+
|
39
|
+
正直ググれば分かると舐めていたのですが、見つかりません…
|
40
|
+
|
25
|
-
|
41
|
+
ご協力お願い致します。
|
26
42
|
|
27
43
|
|
28
44
|
|
29
|
-
|
45
|
+
import requests
|
30
46
|
|
31
|
-
|
47
|
+
from bs4 import BeautifulSoup
|
32
48
|
|
33
|
-
|
49
|
+
import time
|
50
|
+
|
51
|
+
from datetime import datetime
|
34
52
|
|
35
53
|
|
36
54
|
|
55
|
+
|
56
|
+
|
57
|
+
def lineNotify(message):
|
58
|
+
|
59
|
+
line_notify_token = "###以下APIトークン入力"
|
60
|
+
|
61
|
+
line_notify_api = 'https://notify-api.line.me/api/notify'
|
62
|
+
|
63
|
+
payload = {'message': message}
|
64
|
+
|
65
|
+
headers = {'Authorization': 'Bearer ' + line_notify_token}
|
66
|
+
|
67
|
+
requests.post(line_notify_api, data=payload, headers=headers)
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
def get_shortenURL(longUrl):
|
72
|
+
|
73
|
+
url = 'https://api-ssl.bitly.com/v3/shorten'
|
74
|
+
|
75
|
+
access_token = '###以下APIトークン入力'
|
76
|
+
|
37
|
-
|
77
|
+
query = {
|
78
|
+
|
79
|
+
'access_token': access_token,
|
80
|
+
|
81
|
+
'longurl':longUrl
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
r = requests.get(url,params=query).json()['data']['url']
|
86
|
+
|
87
|
+
return r
|
38
88
|
|
39
89
|
|
40
90
|
|
91
|
+
|
92
|
+
|
93
|
+
res = requests.get('https://news.google.com/###以下目的のページのURLが続きます。)
|
94
|
+
|
41
|
-
|
95
|
+
res.raise_for_status()
|
96
|
+
|
97
|
+
soup = BeautifulSoup(res.text, "html.parser")
|
98
|
+
|
99
|
+
elems = soup.select("h3 a") ### 目的のHTMLコード指しています。
|
42
100
|
|
43
101
|
|
44
102
|
|
103
|
+
for elem in elems:
|
104
|
+
|
105
|
+
title= '{}'.format(elem.getText())
|
106
|
+
|
45
|
-
|
107
|
+
add= elem.get('href')
|
46
108
|
|
47
109
|
|
48
110
|
|
111
|
+
address = "(https://news.google.com"+ add +"?hl=en-US&gl=US&ceid=US%3Aen)" ###アメリカのGooglenewsから引き抜きたかったのでこうなってます。
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
urler = get_shortenURL(address)
|
116
|
+
|
117
|
+
lineNotify("\n●"+title+"\n"+urler) ### LINEに短くしたURLを送りたかった。
|
118
|
+
|
49
|
-
|
119
|
+
time.sleep(60) ###仮に60秒ごとにくりかえすようにしているだけです。
|