回答編集履歴

1

内容修正

2018/08/25 06:47

投稿

spookybird
spookybird

スコア1803

test CHANGED
@@ -1,4 +1,6 @@
1
- `then`の中で要素の`value`に設定すればいいだな気がしますがそれではダメでょうか?
1
+ コメントを受て内容を修正てみました。
2
+
3
+ これでどうですか?
2
4
 
3
5
 
4
6
 
@@ -10,32 +12,38 @@
10
12
 
11
13
 
12
14
 
13
- Promise.resolve(
15
+ nightmare
14
16
 
15
- nightmare
17
+ .goto('https://www.yodobashi.com/product/100000001002780005/')
16
18
 
17
- .goto('https://www.yodobashi.com/product/100000001002780005/')
19
+ .wait('.productPrice.fs16.alignM')
18
20
 
19
- .wait('.productPrice.fs16.alignM')
21
+ .evaluate(() => {
20
22
 
21
- .evaluate(() => document.querySelector('.productPrice.fs16.alignM').innerText)
23
+ const value = document.querySelector('.productPrice.fs16.alignM').innerText;
22
24
 
23
- ).then((result) => {
25
+ const element = document.querySelector(/* 値を設定したい対象のセレクタ */);
24
26
 
25
- const element = document.querySelector('*******');
26
-
27
- element.value = result;
27
+ element.value = value;
28
-
29
- }).catch(console.error);
30
28
 
31
29
 
32
30
 
31
+ return value; /* then の resultに渡したいもの。なんでもいい */
32
+
33
+ })
34
+
35
+ .end()
36
+
37
+ .then((result) => {
38
+
39
+ // 取得結果を受けてやりたいことを書く
40
+
41
+ // スクレイピング対象ページ内の操作はここではできない
42
+
43
+ console.log(result);
44
+
45
+ })
46
+
33
- nightmare.end();
47
+ .catch(console.error);
34
48
 
35
49
  ```
36
-
37
-
38
-
39
- コードのインデントがまったくなくて読み解くの大変だったんですが、構造これであってます?
40
-
41
- インデントなしでよく読めますね。