回答編集履歴
1
サンプルソースの追加
test
CHANGED
@@ -7,3 +7,105 @@
|
|
7
7
|
「jsoup使い方メモ」
|
8
8
|
|
9
9
|
http://qiita.com/opengl-8080/items/d4864bbc335d1e99a2d7
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
ログインの方法のサンプルソース
|
16
|
+
|
17
|
+
出典:「JsoupでTwitterにログインしてツイート取得」http://blog.kikudai.com/2014/02/15/846/
|
18
|
+
|
19
|
+
```java
|
20
|
+
|
21
|
+
String ua = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36";
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
try {
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
// 初回画面(hidden、トークンを取得)
|
30
|
+
|
31
|
+
Response res1 = Jsoup.connect("https://twitter.com/")
|
32
|
+
|
33
|
+
.userAgent(ua)
|
34
|
+
|
35
|
+
.execute();
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
Elements elements = res1
|
40
|
+
|
41
|
+
.parse()
|
42
|
+
|
43
|
+
.getElementsByTag("input");
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
// hiddenをMapにつめつめ
|
48
|
+
|
49
|
+
Map<String, String> hiddens = new HashMap<>();
|
50
|
+
|
51
|
+
for (Element el : elements) {
|
52
|
+
|
53
|
+
if (StringUtil.equals(el.attr("type"), "hidden")
|
54
|
+
|
55
|
+
&& StringUtil.isNotEmpty(el.attr("name"))) {
|
56
|
+
|
57
|
+
hiddens.put(el.attr("name"), el.attr("value"));
|
58
|
+
|
59
|
+
}
|
60
|
+
|
61
|
+
}
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
// ログイン
|
66
|
+
|
67
|
+
Response res2 = Jsoup.connect("https://twitter.com/sessions")
|
68
|
+
|
69
|
+
.userAgent(ua)
|
70
|
+
|
71
|
+
.cookies(res1.cookies())
|
72
|
+
|
73
|
+
.data("session[username_or_email]", "アカウント")
|
74
|
+
|
75
|
+
.data("session[password]", "パスワード")
|
76
|
+
|
77
|
+
.data(hiddens)
|
78
|
+
|
79
|
+
.followRedirects(false)
|
80
|
+
|
81
|
+
.method(Connection.Method.POST)
|
82
|
+
|
83
|
+
.execute();
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
Document timeline = Jsoup.connect("https://twitter.com/")
|
88
|
+
|
89
|
+
.userAgent(ua)
|
90
|
+
|
91
|
+
.cookies(res2.cookies())
|
92
|
+
|
93
|
+
.get();
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
System.out.println(timeline);
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
} catch (IOException e) {
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
e.printStackTrace();
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
```
|