TIL 191118_4

김상훈·2019년 11월 18일
0

Crawling with Python!

BeautifulSoup4, urllib 이용

예전에 로스트아크를 즐겨했을 때 인벤에서 주로 정보를 얻었는데
이슈 토론 게시판이 무척 지저분했던 기억이 나서 한번 확인해보았더니
여전히...

한번 크롤링 해보겠슴다.

# reference: https://tariat.tistory.com/4

from urllib.request import urlopen
from bs4 import BeautifulSoup

url = 'http://www.inven.co.kr/board/lostark/5353/171514'

result = urlopen(url)
html = result.read()
soup = BeautifulSoup(html, 'html.parser')

temp1 = soup.select('#tbArticle > div.articleMain > div.articleSubject > div.articleTitle > h1')
temp2 = soup.select('#powerbbsContent')

print('제목: ', temp1[0].get_text())
print('내용: ', temp2[0].get_text())

이 코드를 적용할 때에는 url과 soup.select 이하는 바꾸어줘야 한다.

이유 1.
더러운 글을 긁어왔음...ㅎㅎ;
실행하면 기분이 안좋을 수도 있습니다

이유 2.
만약 주소가 다르다면,
soup.select 이하도 달라져야 한다.
왜냐하면 사이트마다 html 구성이 다르며,
지금 soup.select 이하는 url의 웹페이지에 맞춰 작성되어있기 때문

꿀팁 하나.
크롬 개발자 도구 elements에서
크롤링을 원하는 html element에서 마우스 우클릭을 하면
copy selector이 가능하다.

profile
남과 비교하지 말자.

0개의 댓글