teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

6

コードの再修正・・・未解決

2020/10/17 12:52

投稿

Dantesu
Dantesu

スコア8

title CHANGED
File without changes
body CHANGED
@@ -1,6 +1,15 @@
1
- 検索サイト(*規約確認済)内で、情報をスクレイピングしたいのですが、調べながら作成した以下のコードでは次ページの情報まで取得できません。(10/10 23:38)listにテキスト抽出命じので、ページ遷移箇所を書き直たら今度はプログラムが終わりません。
1
+ 検索サイト(*規約確認済)内で、情報をスクレイピングしたいのですが、調べながら作成した以下のコードでは次ページの情報まで取得できません。(10/17 21:48)コード再度改めみましエラーは出なくなりましたが、ページ遷移しません。中身も取れていません。
2
+
3
+ (10/10 23:38)
4
+ listにテキスト抽出を命じていたので、ページ遷移箇所を書き直したら今度はプログラムが終わりません。
2
5
  どうぞ宜しくお願いします。
3
- ```python
6
+ ```python 10/17編集済み
7
+ import time
8
+ from selenium import webdriver
9
+ driver=webdriver.Chrome()
10
+
11
+ driver.get('https://www.mrso.jp/searches/?redirect&view=plan')
12
+
4
13
  def search(driver):
5
14
  i = 1 # ループ番号、ページ番号を定義
6
15
  i_max = 5 # 最大何ページまで分析するかを定義
@@ -25,21 +34,16 @@
25
34
 
26
35
  # 「次へ」は1つしかないが、あえてelementsで複数検索。空のリストであれば最終ページの意味になる。
27
36
  for elem in class_group:
28
- next=elem.find_elements_by_class_name('-item -next')
37
+ next_list=elem.find_elements_by_class_name('-item -next')
29
- if next==[]:
38
+ if next_list==[]:
30
- i = i_max + 1
39
+ i = i_max + 1
31
40
  else:
32
- # 次ページのURLは-item -nextのhref属性
33
- for elem in class_group:
41
+ next_list.click()
34
- next_page = elem.find_elements_by_class_name('-item -next').get_attribute('href')
35
- driver.get(next_page) # 次ページへ遷移する
36
- i = i + 1 # iを更新
42
+ i = i + 1 # iを更新
37
43
  time.sleep(3) # 3秒間待機
38
44
  return courses_list,facili_list, price_list,link_list # タイトルとリンクのリストを戻り値に指定
39
45
 
46
+ courses_list,facili_list,price_list,link_list=search(driver)
40
47
 
41
48
 
42
- courses_list,facili_list,price_list,link_list=search(driver)
43
- search.quit()
44
-
45
49
  ```

5

頂いた回答をヒントに、プログラムを修正しました。

2020/10/17 12:52

投稿

Dantesu
Dantesu

スコア8

title CHANGED
File without changes
body CHANGED
@@ -1,18 +1,6 @@
1
- 検索サイト(*規約確認済)内で、情報をスクレイピングしたいのですが、調べながら作成した以下のコードでは次ページの情報まで取得できません。単一ページでは取得できましたが、次へボタンの遷移箇所を挿入すると下記のエ
1
+ 検索サイト(*規約確認済)内で、情報をスクレイピングしたいのですが、調べながら作成した以下のコードでは次ページの情報まで取得できません。(10/10 23:38)listにテキスト抽出を命じていたので、ページ遷移箇所を書き直したら今度はプログ終わりせん
2
- invalid selector: Compound class names not permitted
3
2
  どうぞ宜しくお願いします。
4
3
  ```python
5
- Jypterで作成しています。Jupyter上で次の行に入力している箇所は下記のコードで2行空けています。
6
- courses_listで始まる塊でenterを押すとエラーが出ます。
7
-
8
- import time
9
- from selenium import webdriver
10
- driver=webdriver.Chrome()
11
-
12
-
13
- driver.get('https://www.mrso.jp/searches/?redirect&view=plan')
14
-
15
-
16
4
  def search(driver):
17
5
  i = 1 # ループ番号、ページ番号を定義
18
6
  i_max = 5 # 最大何ページまで分析するかを定義
@@ -20,11 +8,12 @@
20
8
  facili_list=[]
21
9
  price_list=[]
22
10
  link_list=[]
11
+ next_list=[]
23
12
 
24
13
  # 現在のページが指定した最大分析ページを超えるまでループする
25
14
  while i <= i_max:
26
- class_group =driver.find_elements_by_class_name('page-search__wrap facility')
15
+ class_group =driver.find_elements_by_class_name('page-search__wrap.facility')
27
- # コース名、施設名、価格、リンクを抽出しリストに追加するforループ
16
+ # タイトルとリンクを抽出しリストに追加するforループ
28
17
  for elem in class_group:
29
18
  courses_list.append(elem.find_element_by_class_name('-name').text)
30
19
  for elem in class_group:
@@ -35,17 +24,21 @@
35
24
  link_list.append(elem.find_element_by_class_name('-link').get_attribute('href'))
36
25
 
37
26
  # 「次へ」は1つしかないが、あえてelementsで複数検索。空のリストであれば最終ページの意味になる。
27
+ for elem in class_group:
38
- if class_group.find_elements_by_class_name('-item -next') == []:
28
+ next=elem.find_elements_by_class_name('-item -next')
29
+ if next==[]:
39
- i = i_max + 1
30
+ i = i_max + 1
40
31
  else:
41
32
  # 次ページのURLは-item -nextのhref属性
33
+ for elem in class_group:
42
- next_page = class_group.find_elements_by_class_name('-item -next').get_attribute('href')
34
+ next_page = elem.find_elements_by_class_name('-item -next').get_attribute('href')
43
- class_group.get(next_page) # 次ページへ遷移する
35
+ driver.get(next_page) # 次ページへ遷移する
44
- i = i + 1 # iを更新
36
+ i = i + 1 # iを更新
45
37
  time.sleep(3) # 3秒間待機
46
- return courses_list,facili_list, price_list,link_list # コース名、施設名、価格、リンクを戻り値に指定
38
+ return courses_list,facili_list, price_list,link_list # タイトルとリンクのリストを戻り値に指定
47
39
 
48
40
 
41
+
49
42
  courses_list,facili_list,price_list,link_list=search(driver)
50
43
  search.quit()
51
44
 

4

誤字

2020/10/10 14:39

投稿

Dantesu
Dantesu

スコア8

title CHANGED
@@ -1,1 +1,1 @@
1
- ごsスクレイピング:検索サイト内での次ページへの遷移
1
+ webスクレイピング:検索サイト内での次ページへの遷移
body CHANGED
File without changes

3

誤植

2020/10/10 08:22

投稿

Dantesu
Dantesu

スコア8

title CHANGED
@@ -1,1 +1,1 @@
1
- スクレイピング:検索サイト内での次ページへの遷移
1
+ ごsスクレイピング:検索サイト内での次ページへの遷移
body CHANGED
@@ -43,7 +43,7 @@
43
43
  class_group.get(next_page) # 次ページへ遷移する
44
44
  i = i + 1 # iを更新
45
45
  time.sleep(3) # 3秒間待機
46
- return courses_list,facili_list, price_list,link_list # タイトルとリンクのリストを戻り値に指定
46
+ return courses_list,facili_list, price_list,link_list # コース名、施設名、価格、リンクを戻り値に指定
47
47
 
48
48
 
49
49
  courses_list,facili_list,price_list,link_list=search(driver)

2

誤植

2020/10/10 08:22

投稿

Dantesu
Dantesu

スコア8

title CHANGED
File without changes
body CHANGED
@@ -24,7 +24,7 @@
24
24
  # 現在のページが指定した最大分析ページを超えるまでループする
25
25
  while i <= i_max:
26
26
  class_group =driver.find_elements_by_class_name('page-search__wrap facility')
27
- # タイトルとリンクを抽出しリストに追加するforループ
27
+ # コース名、施設名、価格、リンクを抽出しリストに追加するforループ
28
28
  for elem in class_group:
29
29
  courses_list.append(elem.find_element_by_class_name('-name').text)
30
30
  for elem in class_group:

1

読みにくかったため改行の挿入

2020/10/10 08:21

投稿

Dantesu
Dantesu

スコア8

title CHANGED
File without changes
body CHANGED
@@ -2,7 +2,8 @@
2
2
  invalid selector: Compound class names not permitted
3
3
  どうぞ宜しくお願いします。
4
4
  ```python
5
- Jypterで作成しています。Jupyter上で次の行に入力している箇所は下記のコードで2行空けています。courses_listで始まる塊でenterを押すとエラーが出ます。
5
+ Jypterで作成しています。Jupyter上で次の行に入力している箇所は下記のコードで2行空けています。
6
+ courses_listで始まる塊でenterを押すとエラーが出ます。
6
7
 
7
8
  import time
8
9
  from selenium import webdriver