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

質問編集履歴

1

投稿時のteratailの不具合のため

2019/10/30 16:17

投稿

banao
banao

スコア13

title CHANGED
File without changes
body CHANGED
@@ -1,5 +1,7 @@
1
- 無限スクロールページをスクレイピングしているのですが、途中で動作が重くなってしまいます。
1
+ 無限スクロールページをスクレイピングしているのですが、途中で動作が重くなってしまいます。
2
+ 以下が動かしているコードの一部です。
3
+ python初心者なため、稚拙な記述の仕方かもしれませんがご容赦くださいm(__)m
2
- ```ここに言語を入力
4
+ ```python
3
5
  from selenium.webdriver import Chrome, ChromeOptions, Remote
4
6
  from selenium.webdriver.common.by import By
5
7
  from selenium.webdriver.support import expected_conditions as EC
@@ -27,5 +29,70 @@
27
29
  time.sleep(2)
28
30
  contents = scrape_contents(driver) # 内容をスクレイピング
29
31
 
32
+ # スクレイピング箇所まで移動
33
+ def navigate(driver):
34
+ driver.get(access_url)
35
+ input_element = driver.find_element_by_name('tid')
36
+ input_element.send_keys(login_ID)
37
+ input_element = driver.find_element_by_name('tpasswd')
38
+ input_element.send_keys(login_pass)
39
+ driver.find_element_by_class_name('MdBtn01').click()
40
+ time.sleep(2)
41
+ driver.find_element_by_class_name('MdBtn01').click()
42
+ time.sleep(2)
43
+
44
+ # スクレイピング
45
+ def scrape_contents(driver):
46
+ contents = []
47
+ n = 1
48
+ limit = 3000
49
+ while n <= limit:
50
+ article = driver.find_elements_by_css_selector(f'div.container > section > article:nth-of-type({n})')
51
+ try: article = article[0]
52
+ except: break
53
+ try:
54
+ post_time = article.find_element_by_css_selector('dd.time > a').text
55
+ post_text = article.find_element_by_css_selector('div.article_contents > p.type_text').text
56
+ post_comment = []
57
+ for c in article.find_elements_by_css_selector('dd.comment > p > span'):
58
+ post_comment.append(c.text)
59
+ post_pic_style = []
60
+ post_pic = []
61
+ for p in article.find_elements_by_css_selector('div.article_contents > div > div > span > a > span[style]'):
62
+ post_pic_style.append(p)
63
+ for b in post_pic_style:
64
+ b = b.value_of_css_property('background-image')
65
+ b = re.findall('"(.*)"',b)
66
+ b = ','.join(b)
67
+ post_pic.append(b)
68
+ except NoSuchElementException:
69
+ post_time = ''
70
+ post_text = ''
71
+ post_comment = ''
72
+ post_pic = ''
73
+ contents.append({
74
+ "time": post_time,
75
+ "text": post_text,
76
+ "comment": post_comment,
77
+ "pic": post_pic,
78
+ })
79
+ if n % 10 == 0:
80
+ try:
81
+ driver.execute_script('scroll(0, document.body.scrollHeight)')
82
+ wait = WebDriverWait(driver, 100)
83
+ wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, f'div.container > section > article:nth-of-type({n+1})')))
84
+ except:
85
+ break
86
+ sys.stdout.write("\r投稿情報取得数:%d" % n)
87
+ sys.stdout.flush()
88
+ n = n + 1
89
+ return contents
90
+
30
- # 目標
91
+ # 実行
92
+ if __name__ == '__main__':
93
+ main()
31
- ```
94
+ ```
95
+ 自動操作でアクセスしたページで投稿(article)の投稿日時、内容、画像URL、返信コメントをスクレイピングしています。articleの要素は最初は10個あり、下までスクロールすると新たに10個読み込まれます。
96
+ limitでスクレイピングする投稿数を指定しています。
97
+ 上のコードの場合は3000回ループさせていますが、2000回あたりで動作の重さが顕著になっていき、3000当たりではほぼ止まってタイムアウトすることもあります。
98
+ 原因はchromeで読み込んでいるページが大きくなりすぎているからでしょうか?また、10000回ほどループさせたいのですが、どうすればタイムアウト(100秒まで)せずに処理できるでしょうか?