質問編集履歴

1

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

2019/10/30 16:17

投稿

banao
banao

スコア13

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,10 @@
1
- 無限スクロールページをスクレイピングしているのですが、途中で動作が重くなってしまいます。
1
+ 無限スクロールページをスクレイピングしているのですが、途中で動作が重くなってしまいます。
2
2
 
3
+ 以下が動かしているコードの一部です。
4
+
5
+ python初心者なため、稚拙な記述の仕方かもしれませんがご容赦くださいm(__)m
6
+
3
- ```ここに言語を入力
7
+ ```python
4
8
 
5
9
  from selenium.webdriver import Chrome, ChromeOptions, Remote
6
10
 
@@ -56,6 +60,136 @@
56
60
 
57
61
 
58
62
 
63
+ # スクレイピング箇所まで移動
64
+
65
+ def navigate(driver):
66
+
67
+ driver.get(access_url)
68
+
69
+ input_element = driver.find_element_by_name('tid')
70
+
71
+ input_element.send_keys(login_ID)
72
+
73
+ input_element = driver.find_element_by_name('tpasswd')
74
+
75
+ input_element.send_keys(login_pass)
76
+
77
+ driver.find_element_by_class_name('MdBtn01').click()
78
+
79
+ time.sleep(2)
80
+
81
+ driver.find_element_by_class_name('MdBtn01').click()
82
+
83
+ time.sleep(2)
84
+
85
+
86
+
87
+ # スクレイピング
88
+
89
+ def scrape_contents(driver):
90
+
91
+ contents = []
92
+
93
+ n = 1
94
+
95
+ limit = 3000
96
+
97
+ while n <= limit:
98
+
99
+ article = driver.find_elements_by_css_selector(f'div.container > section > article:nth-of-type({n})')
100
+
101
+ try: article = article[0]
102
+
103
+ except: break
104
+
105
+ try:
106
+
107
+ post_time = article.find_element_by_css_selector('dd.time > a').text
108
+
109
+ post_text = article.find_element_by_css_selector('div.article_contents > p.type_text').text
110
+
111
+ post_comment = []
112
+
113
+ for c in article.find_elements_by_css_selector('dd.comment > p > span'):
114
+
115
+ post_comment.append(c.text)
116
+
117
+ post_pic_style = []
118
+
119
+ post_pic = []
120
+
121
+ for p in article.find_elements_by_css_selector('div.article_contents > div > div > span > a > span[style]'):
122
+
123
+ post_pic_style.append(p)
124
+
125
+ for b in post_pic_style:
126
+
127
+ b = b.value_of_css_property('background-image')
128
+
129
+ b = re.findall('"(.*)"',b)
130
+
131
+ b = ','.join(b)
132
+
133
+ post_pic.append(b)
134
+
135
+ except NoSuchElementException:
136
+
137
+ post_time = ''
138
+
139
+ post_text = ''
140
+
141
+ post_comment = ''
142
+
143
+ post_pic = ''
144
+
145
+ contents.append({
146
+
147
+ "time": post_time,
148
+
149
+ "text": post_text,
150
+
151
+ "comment": post_comment,
152
+
153
+ "pic": post_pic,
154
+
155
+ })
156
+
157
+ if n % 10 == 0:
158
+
159
+ try:
160
+
161
+ driver.execute_script('scroll(0, document.body.scrollHeight)')
162
+
163
+ wait = WebDriverWait(driver, 100)
164
+
165
+ wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, f'div.container > section > article:nth-of-type({n+1})')))
166
+
167
+ except:
168
+
169
+ break
170
+
171
+ sys.stdout.write("\r投稿情報取得数:%d" % n)
172
+
173
+ sys.stdout.flush()
174
+
175
+ n = n + 1
176
+
177
+ return contents
178
+
179
+
180
+
59
- # 目標
181
+ # 実行
182
+
183
+ if __name__ == '__main__':
184
+
185
+ main()
60
186
 
61
187
  ```
188
+
189
+ 自動操作でアクセスしたページで投稿(article)の投稿日時、内容、画像URL、返信コメントをスクレイピングしています。articleの要素は最初は10個あり、下までスクロールすると新たに10個読み込まれます。
190
+
191
+ limitでスクレイピングする投稿数を指定しています。
192
+
193
+ 上のコードの場合は3000回ループさせていますが、2000回あたりで動作の重さが顕著になっていき、3000当たりではほぼ止まってタイムアウトすることもあります。
194
+
195
+ 原因はchromeで読み込んでいるページが大きくなりすぎているからでしょうか?また、10000回ほどループさせたいのですが、どうすればタイムアウト(100秒まで)せずに処理できるでしょうか?