質問編集履歴
3
削除の取り消し
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,17 +1,183 @@
|
|
1
|
-
|
1
|
+
twitterのAPIを使わずにtweetしようと考えているのですが、tweetされません。
|
2
2
|
|
3
|
+
実行すると
|
4
|
+
|
5
|
+
[+] ログイン完了 HTTP ステータスコード:
|
6
|
+
|
7
|
+
302
|
8
|
+
|
9
|
+
[+]ツイート完了
|
10
|
+
|
11
|
+
HTTPステータスコード:
|
12
|
+
|
13
|
+
200
|
14
|
+
|
15
|
+
となりますが、tweetされてません。
|
16
|
+
|
3
|
-
|
17
|
+
どこか間違えがあれば教えて頂きたいです。
|
4
18
|
|
5
19
|
|
6
20
|
|
7
21
|
|
8
22
|
|
23
|
+
```python
|
24
|
+
|
25
|
+
from bs4 import BeautifulSoup
|
26
|
+
|
27
|
+
from requests.exceptions import ConnectionError
|
28
|
+
|
29
|
+
import requests
|
30
|
+
|
31
|
+
import sys
|
9
32
|
|
10
33
|
|
11
34
|
|
35
|
+
class TwitterTweet:
|
36
|
+
|
37
|
+
def __init__(self):
|
38
|
+
|
39
|
+
self.username = "名前"
|
40
|
+
|
41
|
+
self.password = "パスワード"
|
12
42
|
|
13
43
|
|
14
44
|
|
45
|
+
def tweet(self):
|
46
|
+
|
47
|
+
session = requests.Session()
|
15
48
|
|
16
49
|
|
50
|
+
|
51
|
+
headers = {
|
52
|
+
|
53
|
+
"user-agent": "Mozilla/5.0",
|
54
|
+
|
55
|
+
"accept":"text/html,application/xhtml+xml,application/xml;",
|
56
|
+
|
57
|
+
"accept-language":"ja,en-US;q=0.8,en;q=0.6",
|
58
|
+
|
59
|
+
"content-type":"application/x-www-form-urlencoded",
|
60
|
+
|
61
|
+
"origin":"https://twitter.com/",
|
62
|
+
|
63
|
+
"referer":"https://twitter.com/",
|
64
|
+
|
65
|
+
"upgrade-insecure-requests":"1"
|
66
|
+
|
67
|
+
}
|
68
|
+
|
69
|
+
payload = {
|
70
|
+
|
71
|
+
"session[username_or_email]":"",
|
72
|
+
|
73
|
+
"session[password]":"",
|
74
|
+
|
75
|
+
"remember_me":"1",
|
76
|
+
|
77
|
+
"return_to_ssl":"true",
|
78
|
+
|
79
|
+
"scribe_log":"",
|
80
|
+
|
81
|
+
"redirect_after_login":"/",
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
tweet = {
|
86
|
+
|
87
|
+
"authenticity_token":"",
|
88
|
+
|
89
|
+
"is_permalink_page":"false",
|
90
|
+
|
91
|
+
"page_context":"profile",
|
92
|
+
|
93
|
+
"place_id":"",
|
94
|
+
|
95
|
+
"status":"APIを使用していないツイート",
|
96
|
+
|
97
|
+
"tagged_users":""
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
try:
|
104
|
+
|
105
|
+
response = session.get('https://twitter.com/',headers=headers,allow_redirects=False)
|
106
|
+
|
107
|
+
soup = BeautifulSoup(response.text,"lxml")
|
108
|
+
|
109
|
+
auth_token = soup.find(attrs={'name': 'authenticity_token'}).get('value')
|
110
|
+
|
111
|
+
print (auth_token)
|
112
|
+
|
17
|
-
|
113
|
+
except ConnectionError:
|
114
|
+
|
115
|
+
print ("[\*]Twitterへ接続できません")
|
116
|
+
|
117
|
+
sys.exit()
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
payload["authenticity_token"] = auth_token
|
122
|
+
|
123
|
+
payload["session[username_or_email]"] = self.username
|
124
|
+
|
125
|
+
payload["session[password]"] = self.password
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
try:
|
130
|
+
|
131
|
+
login = session.post('https://twitter.com/sessions',headers=headers,data=payload,allow_redirects=False)
|
132
|
+
|
133
|
+
if login.status_code ==302:
|
134
|
+
|
135
|
+
print ("[+] ログイン完了 HTTP ステータスコード:"),
|
136
|
+
|
137
|
+
print (login.status_code)
|
138
|
+
|
139
|
+
else:
|
140
|
+
|
141
|
+
print("[+] ログイン失敗 HTTP ステータスコード:"),
|
142
|
+
|
143
|
+
print(login.status_code)
|
144
|
+
|
145
|
+
except:
|
146
|
+
|
147
|
+
"[+]ログイン中に通信エラー"
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
try:
|
152
|
+
|
153
|
+
tweet = session.post("https://twitter.com/i/tweet/create",data=tweet,allow_redirects=True,headers=headers,cookies=login.cookies
|
154
|
+
|
155
|
+
)
|
156
|
+
|
157
|
+
if tweet.status_code ==200:
|
158
|
+
|
159
|
+
print("[+]ツイート完了")
|
160
|
+
|
161
|
+
print("HTTPステータスコード:")
|
162
|
+
|
163
|
+
print(tweet.status_code)
|
164
|
+
|
165
|
+
else:
|
166
|
+
|
167
|
+
print("[+]ツイート失敗 HTTPステータスコード:")
|
168
|
+
|
169
|
+
print(tweet.status_code)
|
170
|
+
|
171
|
+
except:
|
172
|
+
|
173
|
+
print("ツイート中にエラー")
|
174
|
+
|
175
|
+
if __name__ == "__main__":
|
176
|
+
|
177
|
+
TwiTweet = TwitterTweet()
|
178
|
+
|
179
|
+
TwiTweet.tweet()
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
```
|
2
誤字
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,183 +1,17 @@
|
|
1
|
-
|
1
|
+
自己解決済みです。
|
2
2
|
|
3
|
-
実行すると
|
4
|
-
|
5
|
-
[+] ログイン完了 HTTP ステータスコード:
|
6
|
-
|
7
|
-
302
|
8
|
-
|
9
|
-
[+]ツイート完了
|
10
|
-
|
11
|
-
HTTPステータスコード:
|
12
|
-
|
13
|
-
200
|
14
|
-
|
15
|
-
となりますが、tweetされてません。
|
16
|
-
|
17
|
-
|
3
|
+
削除機能が無いのでこのままにします。
|
18
4
|
|
19
5
|
|
20
6
|
|
21
7
|
|
22
8
|
|
23
|
-
```python
|
24
|
-
|
25
|
-
from bs4 import BeautifulSoup
|
26
|
-
|
27
|
-
from requests.exceptions import ConnectionError
|
28
|
-
|
29
|
-
import requests
|
30
|
-
|
31
|
-
import sys
|
32
9
|
|
33
10
|
|
34
11
|
|
35
|
-
class TwitterTweet:
|
36
|
-
|
37
|
-
def __init__(self):
|
38
|
-
|
39
|
-
self.username = "名前"
|
40
|
-
|
41
|
-
self.password = "パスワード"
|
42
12
|
|
43
13
|
|
44
14
|
|
45
|
-
def tweet(self):
|
46
|
-
|
47
|
-
session = requests.Session()
|
48
15
|
|
49
16
|
|
50
|
-
|
51
|
-
headers = {
|
52
|
-
|
53
|
-
"user-agent": "Mozilla/5.0",
|
54
|
-
|
55
|
-
"accept":"text/html,application/xhtml+xml,application/xml;",
|
56
|
-
|
57
|
-
"accept-language":"ja,en-US;q=0.8,en;q=0.6",
|
58
|
-
|
59
|
-
"content-type":"application/x-www-form-urlencoded",
|
60
|
-
|
61
|
-
"origin":"https://twitter.com/",
|
62
|
-
|
63
|
-
"referer":"https://twitter.com/",
|
64
|
-
|
65
|
-
"upgrade-insecure-requests":"1"
|
66
|
-
|
67
|
-
}
|
68
|
-
|
69
|
-
payload = {
|
70
|
-
|
71
|
-
"session[username_or_email]":"",
|
72
|
-
|
73
|
-
"session[password]":"",
|
74
|
-
|
75
|
-
"remember_me":"1",
|
76
|
-
|
77
|
-
"return_to_ssl":"true",
|
78
|
-
|
79
|
-
"scribe_log":"",
|
80
|
-
|
81
|
-
"redirect_after_login":"/",
|
82
|
-
|
83
|
-
}
|
84
|
-
|
85
|
-
tweet = {
|
86
|
-
|
87
|
-
"authenticity_token":"",
|
88
|
-
|
89
|
-
"is_permalink_page":"false",
|
90
|
-
|
91
|
-
"page_context":"profile",
|
92
|
-
|
93
|
-
"place_id":"",
|
94
|
-
|
95
|
-
"status":"APIを使用していないツイート",
|
96
|
-
|
97
|
-
"tagged_users":""
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
}
|
102
|
-
|
103
|
-
try:
|
104
|
-
|
105
|
-
response = session.get('https://twitter.com/',headers=headers,allow_redirects=False)
|
106
|
-
|
107
|
-
soup = BeautifulSoup(response.text,"lxml")
|
108
|
-
|
109
|
-
auth_token = soup.find(attrs={'name': 'authenticity_token'}).get('value')
|
110
|
-
|
111
|
-
print (auth_token)
|
112
|
-
|
113
|
-
|
17
|
+
tweepyなどを使った方がずっと早いですね
|
114
|
-
|
115
|
-
print ("[*]Twitterへ接続できません")
|
116
|
-
|
117
|
-
sys.exit()
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
payload["authenticity_token"] = auth_token
|
122
|
-
|
123
|
-
payload["session[username_or_email]"] = self.username
|
124
|
-
|
125
|
-
payload["session[password]"] = self.password
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
try:
|
130
|
-
|
131
|
-
login = session.post('https://twitter.com/sessions',headers=headers,data=payload,allow_redirects=False)
|
132
|
-
|
133
|
-
if login.status_code ==302:
|
134
|
-
|
135
|
-
print ("[+] ログイン完了 HTTP ステータスコード:"),
|
136
|
-
|
137
|
-
print (login.status_code)
|
138
|
-
|
139
|
-
else:
|
140
|
-
|
141
|
-
print("[+] ログイン失敗 HTTP ステータスコード:"),
|
142
|
-
|
143
|
-
print(login.status_code)
|
144
|
-
|
145
|
-
except:
|
146
|
-
|
147
|
-
"[+]ログイン中に通信エラー"
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
try:
|
152
|
-
|
153
|
-
tweet = session.post("https://twitter.com/i/tweet/create",data=tweet,allow_redirects=True,headers=headers,cookies=login.cookies
|
154
|
-
|
155
|
-
)
|
156
|
-
|
157
|
-
if tweet.status_code ==200:
|
158
|
-
|
159
|
-
print("[+]ツイート完了")
|
160
|
-
|
161
|
-
print("HTTPステータスコード:")
|
162
|
-
|
163
|
-
print(tweet.status_code)
|
164
|
-
|
165
|
-
else:
|
166
|
-
|
167
|
-
print("[+]ツイート失敗 HTTPステータスコード:")
|
168
|
-
|
169
|
-
print(tweet.status_code)
|
170
|
-
|
171
|
-
except:
|
172
|
-
|
173
|
-
print("ツイート中にエラー")
|
174
|
-
|
175
|
-
if __name__ == "__main__":
|
176
|
-
|
177
|
-
TwiTweet = TwitterTweet()
|
178
|
-
|
179
|
-
TwiTweet.tweet()
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
```
|
1
情報の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
APIを使わずにtweetしようと考えているのですが、tweetされません。
|
1
|
+
twitterのAPIを使わずにtweetしようと考えているのですが、tweetされません。
|
2
2
|
|
3
3
|
実行すると
|
4
4
|
|