-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17_selenium_movie_scroll.py
More file actions
83 lines (59 loc) · 2.43 KB
/
17_selenium_movie_scroll.py
File metadata and controls
83 lines (59 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from selenium import webdriver
browser = webdriver.Chrome()
browser.maximize_window()
# 페이지 이동
url = "https://play.google.com/store/movies/top"
browser.get(url)
# 지정한 위치로 스크롤 내리기
# 모니터(해상도) 높이인 1080 위치로 스크롤 내리기
# browser.execute_script("window.scrollTo(0, 1080)") # 1920x1080
# browser.execute_script("window.scrollTo(0, 2080)") # 2080높이만큼 스크롤를 내린다.
# 화면 가장 아래로 스크롤 내리기
# browser.execute_script("window.scrollTo(0, document.body.scrollHeight)")
import time
interval = 2 # 2초에 한번씩 스크롤 내림
# 현재 문서 놀이를 가져와서 저장
prev_height = browser.execute_script("return document.body.scrollHeight")
# 반복 수행
while True:
# 스크롤을 가장 아래로 내림
browser.execute_script("window.scrollTo(0, document.body.scrollHeight)")
# 페이지 로딩 대기
time.sleep(interval)
# 현재 문서 높이를 가져와서 저장
curr_height = browser.execute_script("return document.body.scrollHeight")
if curr_height == prev_height:
break
prev_height = curr_height
print("스크롤 완료")
import requests
from bs4 import BeautifulSoup
res = requests.get(url, headers=headers)
res.raise_for_status()
soup = BeautifulSoup(browser.page_source, "lxml")
# movies = soup.find_all("div", attrs={"class": ["ImZGtf mpg5gc", "Vpfmgd"]})
movies = soup.find_all("div", attrs={"class": "Vpfmgd"})
print(len(movies))
# with open("movie.html", "w", encoding="utf8") as f:
# f.write(soup.prettify()) # .prettify : html 문서를 예쁘게 출력
for movie in movies:
title = movie.find("div", attrs={"class": "WsMG1c nnK0zc"}).get_text()
print(title)
# 할인 전 가격
original_price = movie.find("span", attrs={"class": "SUZt4c djCuy"})
if original_price:
original_price = original_price.get_text()
else:
# print(title, " <할인되지 않은 영화 제외>")
continue
# 할인 된 가격
price = movie.find("span", attrs={"class": "VfPpfd ZdBevf i5DZme"}).get_text()
# 링크
link = movie.find("a", attrs={"class": "JC71ub"})["href"]
# 올바른 링크 : https://play.google.com + link
print(f"제목 : {title}")
print(f"할인 전 금액 : {original_price}")
print(f"할인 후 금액 : {price}")
print("링크 : ", "https://play.google.com" + link)
print("-" * 12)
browser.quit()