반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- vscode venv 설치
- venv 설치
- uipath 입문
- 파이썬 가상환경 설치방법
- 파이썬네이버부동산
- Element is not clickable at point
- Uipath 설치방법
- pycdas.exe
- 날짜 정규식
- 네이버부동산크롤링
- pycdc.exe
- 커머스API
- 네이버커머스API
- 왕초보 파이썬 실행
- UiPath
- 네이버매물크롤링
- 파이썬 환경설정
- pywinauto
- 가상환경설치
- 파이썬 네이버 로그인
- pywinauto 윈도우제어
- 네이버 로그인 하기
- 네이버 로그인 캡챠해결
- selenium
- 네이버 로그인 영수증 해결
- Python
- pywinauto 윈도우
- Uipath 기초
- Selenium 셀렉터잡기
- 파이썬 가상환경 설치
Archives
- Today
- Total
콘솔워크
[나도코딩 웹스크래핑] Chrome headless 최종소스 본문
반응형
나도코딩에서 동적인 동작을 통해서 구글에서 webscraping이 가능하다.
스크롤을 내리면서 전체 할인된 영화정보를 가져오고, 이것을 크롬을 열지 않고도 웹스크래핑이 가능하다.
headless 옵션을 제외하면 foreground로 동작한다.
import time
from bs4 import BeautifulSoup
from selenium import webdriver
options = webdriver.ChromeOptions()
options.headless = True
options.add_argument("window-size=1920x1080")
options.add_argument("user-agent=")
interval = 2 # 2초에 한번 씩 스크롤 내림
browser = webdriver.Chrome(options=options)
browser.maximize_window()
url = "https://play.google.com/store/movies/top"
browser.get(url)
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("스크롤 완료")
browser.get_screenshot_as_file("scrrenshots/google_movie.png")
soup = BeautifulSoup(browser.page_source, "lxml")
movies = soup.find_all("div", attrs={"class": "Vpfmgd"})
for movie in movies:
title = movie.find("div", attrs={"class": "WsMG1c nnK0zc"}).get_text()
original_price = movie.find("span", attrs={"class": "SUZt4c djCuy"})
if original_price:
original_price = original_price.get_text()
else:
continue
# 할인 된 가격
price = movie.find(
"span", attrs={"class": "VfPpfd ZdBevf i5DZme"}).get_text()
# 링크
link = movie.find("a", attrs={"class": "JC71ub"})["href"]
print(f"제목 : {title}")
print(f"할 인전 금액 : {original_price}")
print(f"할인 후 금액 : {price}")
print("링크 : ", f"https://play.google.com{link}")
print("="*120)
반응형
'프로그래밍 > python' 카테고리의 다른 글
[나도코딩 웹스크래핑] 퀴즈1 - 다음 부동산- 헬리오시티 검색 결과 출력 (0) | 2021.01.17 |
---|---|
[나도코딩 웹스크래핑] 정리 (0) | 2021.01.17 |
[Python dataframe] Union and Union ALL (0) | 2021.01.15 |
[Python dataframe] 값이 없는 데이터 filtering notnull (0) | 2021.01.12 |
[Python dataframe] 특정컬럼의 특정 값 변경 (replace) (0) | 2021.01.12 |