본문 바로가기
✨ python/크롤링(Crawling)

파이썬(python) - 크롤링(Crawling) 또는 스크래핑(Scraping)

by 환풍 2023. 5. 11.
728x90

 

먼저, 파이썬에서 크롤링을 처음한다면,

pip install bs4

를 통해 설치를 해줘야한다.

 

 

https://www.pythonscraping.com/pages/warandpeace.html

 

https://www.pythonscraping.com/pages/warandpeace.html

"Well, Prince, so Genoa and Lucca are now just family estates of the Buonapartes. But I warn you, if you don't tell me that this means war, if you still try to defend the infamies and horrors perpetrated by that Antichrist- I really believe he is Antichris

www.pythonscraping.com

 

 

위 홈페이지에 있는 HTML이다.

 

 

from bs4 import BeautifulSoup
from urllib.request import urlopen

 

 

만약 찾는 태그가 여러개라면 제일 처음 나오는 태그만 가져옴.
 빨간 글자를 다 가져와보겠다.
print(bs.find('span'))

모든 span 태그를 다 찾는 방법
bs.find_all('span')

보다시피 [  ]  로 감싸져있다. 즉, List로 받아왔다.

spanTagList = bs.find_all('span', class_='green')

for spanTag in spanTagList :
    print(spanTag.get_text())

 

 

class가 green으로 되어있는것을 변수로 주고, for문으로 돌려줬다.

 

 

반응형

댓글