ํ์ด์ ํฌ๋กค๋ง ์ ๋ฐ์ ์ธ ์ฝ๋ / ํน์ง
์น ๋ธ๋ผ์ฐ์ ๋ฅผ ๋ณด์ง ์๊ณ ํฌ๋กค๋งํ๋ ๋ฐฉ์
2๋ฒ์จฐ ํญ์ผ๋ก ์์ฐฝ์ผ๋ก ์์ฑ
๋ธ๋ญํฌ
์คํฌ๋ํ์ ์ ๋ฌธ์ผ๋ก ํ๋ ๋ชจ๋ ์์ฑ
ํ๋๋ ๋น์ฐฝ์ด๊ณ , ํ๋๋ ํ๊ฒ์ฐฝ
URL๋ก ์คํฌ๋ํํ ๋
๊ดํธ๊ฐ ์์ผ๋ฉด ์ผ๋ถ๋ถ๋ง ์งค๋ผ์ / ํน์ ์ซ์*์ ์ฒด์๋ง ๊ฐ์ ธ์ค๋ ๊ฒฝ์ฐ XPATH
F TYPE ์ผ๋ก ํด๋น URL๋ฐ๋ณต
๋ค์ด๋ฒ ๋ก๊ทธ์ธ ๋ฐฉ๋ฒ 2๊ฐ์ง ๋น๊ต
1. send keys() ๋ก ๊ฐ๋ ๋ฐฉ๋ฒ
driver.implicitly_wait(3)
driver.get('https://nid.naver.com/nidlogin.login')
# ์์ด๋/๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํด์ค๋ค.
driver.find_element_by_name('id').send_keys('naver_id')
driver.find_element_by_name('pw').send_keys('mypassword1234')
# ๋จ์ํ ์ฝ๋ ๋ฐฉ์
# send_keys(Keys.PAGE_DOWN) ํค์ด๋ฒคํธ ๋ฐฉ์
# .send_Keys() ๋ฉ์๋๋ฅผ ์ ์ฉํ๊ธฐ ์ํด์๋ ์์๋ฅผ ๊ฐ์ ธ์์ผ ํ๋ ์ ์ฝ์ด ์์
# javascript๋ฅผ ์ฐ์ง ์๋๊ฒ ํฐ ์ฅ์
label.send_keys(Keys.PAGE_DOWN);
2. execute_script()
-execute_script('js์์ฑ๊ฐ๋ฅ') ๋ฉ์๋๋ฅผ ์ฌ์ฉ
-js์ฝ๋๋ฅผ ์ธ์๋ก ๋ฃ์ ์ ์์ด ๋ธ๋ผ์ฐ์ ํ๋ฉด์ ์ ์ด ํ ์ ์๊ฒ ๋๋ค.
-์คํฌ๋กค์ ํ๊ธฐ ์ํด ๋ฟ๋ง์๋๋ผ ๋ค๋ฅธ ๋์ ์ด๋ฒคํธ๋ฅผ ์์ฑ ๊ฐ๋ฅ
- ํ๋ฉด์ ์คํฌ๋กค ์์น ์ด๋ : scrollTo(x,Y) ,scrollTo(x,Y+number)
- ํ๋ฉด ์ตํ๋จ์ผ๋ก ์คํฌ๋กค ์ด๋ : scrollTo(0, document.body.scrollHeight)
- ํ๋ฉด์ ์์ง์ด๊ณ ํ์ด์ง ๋ก๋ ๊ธฐ๋ค๋ฆฌ๊ธฐ : time.sleep(second)
SCROLL_PAUSE_TIME = 0.5
# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
# Scroll down to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Wait to load page
time.sleep(SCROLL_PAUSE_TIME)
# Calculate new scroll height and compare with last scroll height
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
์ค์นํ์ผ
1. ํฌ๋กฌ ๋ฒ์ ํ์ธ
์ฃผ์์ฐฝ์ :
ChromeDriver - WebDriver for Chrome - Downloads
Current Releases If you are using Chrome version 97, please download ChromeDriver 97.0.4692.20 If you are using Chrome version 96, please download ChromeDriver 96.0.4664.45 If you are using Chrome version 95, please download ChromeDriver 95.0.4638.69 For o
chromedriver.chromium.org
ํด๋น os๋ก ๋ค์ด : window๋ 32๋นํธ
3. vscode์ ํด๋น ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ค์ด
4. ์์ถ์ ํ์ด์ฃผ๊ณ , ์ ๋ ๋ ์์น
์ด ๊ฒฝ๋ก๋ฅผ ๋์ค์ Selenium ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ ์ง์ ํด ์ฃผ์ด์ผ ํ๋ค.
(๊ทธ๋์ผ python์ด chromedriver๋ฅผ ํตํด ํฌ๋กฌ ๋ธ๋ผ์ฐ์ ๋ฅผ ์กฐ์์ด ๊ฐ๋ฅ)
5. headless ์คํํ๊ธฐ ์ํด์ (์์ง ์คํX)
https://beomi.github.io/2017/02/27/HowToMakeWebCrawler-With-Selenium/
๋๋ง์ ์น ํฌ๋กค๋ฌ ๋ง๋ค๊ธฐ(3): Selenium์ผ๋ก ๋ฌด์ ํฌ๋กค๋ฌ ๋ง๋ค๊ธฐ - Beomi's Tech blog
์ข ๋ ๋ณด๊ธฐ ํธํ ๊น๋ถ ๋ฒ์ ์ ๋๋ง์ ์น ํฌ๋กค๋ฌ ๋ง๋ค๊ธฐ๊ฐ ๋์์ต๋๋ค! Updated @ 2019.10.10. Typo/Layout fix, ๋ค์ด๋ฒ ๋ก๊ทธ์ธ Captcha๊ด๋ จ ์์ ์ถ๊ฐ ์ด์ ๊ฒ์๊ธ: ๋๋ง์ ์น ํฌ๋กค๋ฌ ๋ง๋ค๊ธฐ(2): Login with Session Se
beomi.github.io
6.
๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ด๋ จ ๋ฌธ์
1. shutil
https://docs.python.org/ko/3/library/shutil.html
shutil — ๊ณ ์์ค ํ์ผ ์ฐ์ฐ — Python 3.10.0 ๋ฌธ์
shutil — ๊ณ ์์ค ํ์ผ ์ฐ์ฐ ์์ค ์ฝ๋: Lib/shutil.py shutil ๋ชจ๋์ ํ์ผ๊ณผ ํ์ผ ๋ชจ์์ ๋ํ ์ฌ๋ฌ ๊ฐ์ง ๊ณ ์์ค ์ฐ์ฐ์ ์ ๊ณตํฉ๋๋ค. ํนํ, ํ์ผ ๋ณต์ฌ์ ์ญ์ ๋ฅผ ์ง์ํ๋ ํจ์๊ฐ ์ ๊ณต๋ฉ๋๋ค. ๊ฐ๋ณ ํ์ผ
docs.python.org
2. selenium ๋ผ์ด๋ธ๋ฌ๋ฆฌ
-๋ค๋ฅธ ์ฌ๋ฌ ๋ชจ๋๋ค ํฌํจ
https://greeksharifa.github.io/references/2020/10/30/python-selenium-usage/
Python, Machine & Deep Learning
Python, Machine Learning & Deep Learning
greeksharifa.github.io
rom selenium import webdriver # ๋ธ๋ผ์ฐ์ ๋ฅผ ๋์ฐ๊ธฐ ์ํด์ ํ์
from selenium.webdriver.common.keys import Keys # ํค์ด๋ฒคํธ๋ฅผ ์ฃผ๊ธฐ ์ํด ํ์
import time # ์คํฌ๋กค ํ
์ ์ฃผ๊ธฐ ์ํด ํ์
- webdriver๋ firefox, chrome, ie๋ฑ ๋ธ๋ผ์ฐ์ ์ ํ์ด ๊ฐ๋ฅํ๋ค.
- Keysํด๋์ค ๋ ํค๋ณด๋ ํค๋ค์ ์ ๊ณตํ๋ค(e.g. return, f1, alt, cmd, shift ...)
ํจ์ | ์ค๋ช |
webdriver.Chrome("c:/...") | chrome driver๊ฐ ์ค์น๋ ์์น ์ง์ ํ์ฌ ์ฌ์ฉ |
implicitly_wait(5) | ์๋ฌต์ ์ผ๋ก ๋ชจ๋ ์น ์์ ๋ก๋๋ฅผ ์ํด 5์ด ๊ธฐ๋ค๋ฆผ |
get('http://url.com’) | url์ ์ ๊ทผ |
page_source | ํ์ฌ ๋ ๋๋ง ๋ ํ์ด์ง์ Elements๋ฅผ ๋ชจ๋ ๊ฐ์ ธ์ค๊ธฐ |
find_element_by_name('...’) | ํ์ด์ง์ ๋จ์ผ element์ค name์ผ๋ก ์ ๊ทผ |
find_element_by_id('HTML_id’) | id๋ก ์ ๊ทผ |
find_element_by_xpath(‘xpath’) | xpath๋ก ์ ๊ทผ |
find_element_by_css_selector(‘...’) | css selector๋ก ์ ๊ทผ |
find_element_by_class_name('...’) | class ์ด๋ฆ์ผ๋ก ์ ๊ทผ |
find_element_by_tag_name('...’) | tag name์ผ๋ก ์ ๊ทผ |
close | ์ฌ์ฉํ๋ chrome driver ๋ซ๊ธฐ |
xpath ์ด์ฉํ๋ ๋ฒ
https://wkdtjsgur100.github.io/selenium-xpath/
(python) selenium์์ xpath๋ฅผ ์ด์ฉํด ํฌ๋กค๋ง ํ๊ธฐ
selenium์ผ๋ก ํน์ element๋ฅผ ๊ฐ์ ธ์ฌ ๋, ๊ฐ์ ธ์ค๊ณ ์ถ์ element๊ฐ ๋ค๋ฅธ element ์์ ์์ ๊ฒฝ์ฐ์ ๊ทธ ํน์ element๋ฅผ ์ฝ๊ฒ ๊ฐ์ ธ์ฌ ์ ์๋ ๋ฐฉ๋ฒ ์ค ํ๋์ธ xpath๋ฅผ ์ฌ์ฉํด element๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฐฉ๋ฒ์ ๋ํด์ ์
wkdtjsgur100.github.io
3. BeautifulSoup4: HTML parser
- ์ง์ HTML๋ก๋ถํฐ ์ํ๋ ์์น/ํ์์ ๋ฌธ์์ด์ ํ๋
- ์ฃผ๋ก Requests์ ์ํด ๋ง์ด ์ฌ์ฉ๋์ง๋ง, Selenium์์๋ ์ฌ์ฉํ ์ ์๋ค.
ํฌ๋กค๋ง ์์
1. ๋น๋ณด๋ ์ฐจํธ์์ ๊ทธ๋ฆผ(์ด๋ฏธ์ง)ํ์ผ ํฌ๋กค๋งํ๋ ๋ฒ
from bs4 import BeautifulSoup # ์
๋ ๋์์ผ๋ก ์ด๋ฒคํธ๋ฅผ ํ๋ค beautifulsoup์ผ๋ก ์์ ์ ๋ฆฌ, ๊ทธ๋ฅ ์
๋ ๋์์ผ๋ก ํด๋๋๋น
from selenium import webdriver # ๋ธ๋ผ์ฐ์ ๋ฅผ ์ด์ ์๋ ๋๋ผ์ด๋ธ๋ชจ๋
from selenium.webdriver.common.keys import Keys # ํค์ด๋ฒคํธ๋ฅผ ๋๋ ํค ๋ชจ๋
import csv, time
# csv์ ์ฅ
output = 'test_crawling_02.csv'
csv_open = open(output, 'w+', encoding='utf-8')
csv_writer = csv.writer(csv_open)
csv_writer.writerow(('index','title','artist', 'image_url'))
# ํฌ๋กค๋ง url, html ๋ฐ์์ค๊ธฐ
orig_url = 'https://www.billboard.com/charts/billboard-200' # ํฌ๋กค๋ง ํ ์ฌ์ดํธ
driver = webdriver.Chrome('/Users/hwang/dev/chromedriver') # ํฌ๋กฌ ๋ธ๋ผ์ฐ์ ์ ํ
driver.implicitly_wait(10) # ์ซ์ ํฌ๋ฉด ์ ์ฝํ๋ ๊ธฐ๋ถ..
driver.get(orig_url) # ์
๋ ฅํ ๊ฒฝ๋ก์ ์ ๋ณด ๊ธ์ด๋ณผ๊น์~?
body = driver.find_element_by_css_selector('body') # send_keys()๋ฉ์๋ ์ฌ์ฉ์ ์ํ body๊ฐ์ ธ์ค๊ธฐ
for i in range(15): # 11๋ฒ ~ ์ตํ๋จ 20๋ฒ
body.send_keys(Keys.PAGE_DOWN) # ํ์ด์ง ๋ค์ด ํค๋ฅผ 20ํ ๋ฐ๋ณตํ๋ค.
time.sleep(0.1) # ํ์ด์ง ๋ก๋ ๋๊ธฐ, ์ซ์๊ฐ ํฌ๋ฉด ์์ฝํ๋๊ฑด ์ค๊น์?
# beautifulsoup ์ฌ์ฉ ํ๊ธฐ ์ค๋น
html = driver.page_source # html์ ๋ฌธ์์ด๋ก ๊ฐ์ ธ์จ๋ค.
driver.close() # ํฌ๋กฌ๋๋ผ์ด๋ฒ ๋ซ๊ธฐ
# beautifulsoup ์ฌ์ฉํ๊ธฐ
soup = BeautifulSoup(html,'html.parser')
top_200_list = soup.find_all( 'li', {'class' : re.compile('chart-list__element')} )
for li in top_200_list[:100]:
# ํน์๋ผ๋ ์ด๋ฏธ์ง๊ฐ ๋น์์๋๋ฅผ ๋๋น ํ์ฌ ์๋ ํจ์ ์ถ๊ฐ
def check_image_url():
value = bool(li.find('span', {'class':'chart-element__image'})['style'] != 'display: inline-block;')
if value:
return li.find('span', {'class':'chart-element__image'})['style'].split('"')[1]
return None
# * re.compile() ๊ณผ class_='' ์ ์ฐจ์ด๋ ?..
index = li.find('span', {'class':'chart-element__rank__number'}).text
title = li.find('span', {'class':'chart-element__information__song'}).text
artist = li.find('span', {'class':'chart-element__information__artist'}).text
image_url = check_image_url()
# CSV ์ ์ ์ฅํ์
csv_writer.writerow((index, title, artist, image_url))
2. ์ํฐ์น๋ ๋ฐฉ๋ฒ
# ์ฒซ๋ฒ์งธ ๋ฐฉ๋ฒ
sample = browser.find_element_by_css_select('a') sample.send_keys('\n')
# ํด๋น ๋งํฌ/๋ช ๋ น์ด ์ ์ํฐ ๋ฅผ ์คํํ๋๋ก
# ๋ ๋ฒ์งธ ๋ฐฉ๋ฒ
sample = browser.find_element_by_css_select('a') browser.execute_script("arguments[0].click();", sample)
#์๋ฐ ๋ช ๋ น์ด ์คํ
3. ์ผ๋ถ ์ฝ๋
4. ์ ๊ทํํ์
https://ponyozzang.tistory.com/335
Python ์ ๊ท ํํ์(re.sub)์ ์ด์ฉํ ๋ฌธ์์ด ์นํ ๋ฐฉ๋ฒ ๋ฐ ์์
ํ์ด์ฌ์์ ๋ฌธ์์ด์ ์นํํด์ฃผ๋ ๋ฉ์๋๋ก replace๊ฐ ์์ต๋๋ค. ์ด๋ฒ์๋ replace ๋ฉ์๋๋ก ๋ฌธ์์ด์ ์นํํ๋ ๋ฐฉ๋ฒ์ด ์๋ ์ ๊ท ํํ์์ ์ด์ฉํ์ฌ ๋ฌธ์์ด์ ์นํํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค. ์ ๊ท ํํ
ponyozzang.tistory.com
https://lovelydiary.tistory.com/17
ํ์ด์ฌ ํฌ๋กค๋ฌ) ๋ฐ์ดํฐ ํ์ฑ ํ ์ธ๋ฐ์๋ ํ๊ทธ ์ง์ฐ๊ธฐ (re.sub, ํ๊ทธ ์ ๊ฑฐ)
Beautiful Soup์ ํตํด html.parser๋ก ํ์ฑํ ๊ฐ๋ค์ ์ป์๋ค ! soup=BeautifulSoup(html, 'html.parser') prd_names=soup.find_all('td', class_='ProdName') td class๊ฐ ProdName์ผ๋ก ๋ ์ ํ๋ช ๋ค์ ๋ชจ๋ ์ฐพ์๋ด..
lovelydiary.tistory.com
'๐๏ธ์ํํธ์จ์ด > ๐ปpython' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Kernel died with exit code 1073741845 ์๋ฌ (0) | 2021.11.23 |
---|---|
selenium.webdriver.common.keys (0) | 2021.11.23 |
[git/ source tree] ํ์ ํ ์ฌ์ฉ๋ฒ (0) | 2021.11.21 |
vscode ๋จ ์ฌ์ฉ (0) | 2021.11.16 |
[github code]๊นํ๋ธ ํ์ผ ๋ค์ด๋ฐฉ๋ฒ (0) | 2021.11.16 |