파이썬 철학 스크래핑 실습

요청:

https://localprod.pandateacher.com/python-manuscript/hello-spiderman/에서 'Python의 철학' 중영문 버전을 추출하여 출력하는 스크립트 작성

목표:

동적 웹 페이지 크롤링 기법 연습

Selenium과 BeautifulSoup의 통합 활용 연습

URL: https://localprod.pandateacher.com/python-manuscript/hello-spiderman/

방법 1: 순수 Selenium 사용


1 from selenium import webdriver
2 import time
3 
4 driver = webdriver.Chrome()
5 
6 driver.get('https://localprod.pandateacher.com/python-manuscript/hello-spiderman/')
7 time.sleep(2)
8 
9 toggle_button = driver.find_element_by_class_name('sub')
10 toggle_button.click()
11 time.sleep(1)
12 
13 zen_sections = driver.find_elements_by_class_name('content')
14 
15 for section in zen_sections:
16     print(section.find_element_by_tag_name('h1').text + '\n\n')
17     print(section.find_element_by_tag_name('p').text + '\n\n')
18 
19 driver.quit()

1         The Zen of Python
2 
3             Beautiful is better than ugly.
4             Explicit is better than implicit.
5             Simple is better than complex.
6             Complex is better than complicated.
7             Flat is better than nested.
8             Sparse is better than dense.
9             Readability counts.
10             Special cases aren't special enough to break the rules.
11             Although practicality beats purity.
12             Errors should never pass silently.
13             Unless explicitly silenced.
14             In the face of ambiguity, refuse the temptation to guess.
15             There should be one-- and preferably only one --obvious way to do it.
16             Although that way may not be obvious at first unless you're Dutch.
17             Now is better than never.
18             Although never is often better than *right* now.
19             If the implementation is hard to explain, it's a bad idea.
20             If the implementation is easy to explain, it may be a good idea.
21             Namespaces are one honking great idea -- let's do more of those!
22 
23         Python의 철학
24 
25             아름다움은 불整洁보다 나음
26             명확성은 암호화된 코드보다 나음
27             간결함은 복잡함보다 나음
28             복잡성은 혼란스러운 구조보다 나음
29             평평한 구조는 중첩보다 나음
30             간격은 밀집도보다 나음
31             가독성은 최우선 과제임
32             특별한 경우라도 규칙을 어겨서는 안됨
33             실용성은 순수성보다 우선시해야 함
34             오류는 침묵으로 지나가면 안됨
35             명확하게 무시해야 할 때만 무시하라
36             모호할 때는 추측하지 말고 명확한 방식을 선택하라
37             하나의 명확한 방법이 존재해야 한다
38             그 방법이 처음에는 명확하지 않더라도
39             파이썬의 창시자만이 아닌 누구라도 이해할 수 있어야 함

방법 2: Selenium + BeautifulSoup 조합


1 from selenium import webdriver
2 from bs4 import BeautifulSoup
3 import time
4 
5 driver = webdriver.Chrome()
6 
7 driver.get('https://localprod.pandateacher.com/python-manuscript/hello-spiderman/')
8 time.sleep(2)
9 
10 toggle_button = driver.find_element_by_class_name('sub')
11 toggle_button.click()
12 time.sleep(1)
13 
14 html_content = driver.page_source
15 
16 soup = BeautifulSoup(html_content,'html.parser')
17 zen_blocks = soup.find_all(class_='content')
18 for block in zen_blocks:
19     print('\n\t' + block.find('h1').text)
20     print(block.find('p').text)
21 
22 driver.quit()

1         The Zen of Python
2 
3             Beautiful is better than ugly.
4             Explicit is better than implicit.
5             Simple is better than complex.
6             Complex is better than complicated.
7             Flat is better than nested.
8             Sparse is better than dense.
9             Readability counts.
10             Special cases aren't special enough to break the rules.
11             Although practicality beats purity.
12             Errors should never pass silently.
13             Unless explicitly silenced.
14             In the face of ambiguity, refuse the temptation to guess.
15             There should be one-- and preferably only one --obvious way to do it.
16             Although that way may not be obvious at first unless you're Dutch.
17             Now is better than never.
18             Although never is often better than *right* now.
19             If the implementation is hard to explain, it's a bad idea.
20             If the implementation is easy to explain, it may be a good idea.
21             Namespaces are one honking great idea -- let's do more of those!
22 
23         Python의 철학
24 
25             아름다움은 불整洁보다 나음
26             명확성은 암호화된 코드보다 나음
27             간결함은 복잡함보다 나음
28             복잡성은 혼란스러운 구조보다 나음
29             평평한 구조는 중첩보다 나음
30             간격은 밀집도보다 나음
31             가독성은 최우선 과제임
32             특별한 경우라도 규칙을 어겨서는 안됨
33             실용성은 순수성보다 우선시해야 함
34             오류는 침묵으로 지나가면 안됨
35             명확하게 무시해야 할 때만 무시하라
36             모호할 때는 추측하지 말고 명확한 방식을 선택하라
37             하나의 명확한 방법이 존재해야 한다
38             그 방법이 처음에는 명확하지 않더라도
39             파이썬의 창시자만이 아닌 누구라도 이해할 수 있어야 함

태그: selenium BeautifulSoup 웹스크래핑 python 동적컨텐츠

6월 26일 00:57에 게시됨