본문 바로가기
파이썬

[D+7] 개발일지: 크롤링을 배우다

by 개발자A입니다 2022. 4. 13.

2022년 4월 12일 화요일

 

이번 주차 강의는 진도 나가는게 힘들었다.

별것도 아닌 것에 에러가 발생했는데 찾지 못한 날도 있었고, 

이사하느라 공부 자체를 못한 날도 있었다.

 

그래도 끝냈으니 다행이다~!

물론 과제는 혼가 낑낑 거리다가 해설영상을 보고 풀었다ㅎㅎㅎ

 

문법은 배우지않고 바로 실습으로 진행하고 있지만,

뭔가 조금씩 이해가 되고 있는 것 같다ㅎㅎ

물론 이론도 배우면 좋겠지만.. 직장인에게는 이런 방법이 더 좋다ㅎ

 

아래는 결과물과 코드이다~!

복붙만 하다보니 내실력이 아닌 것 같지만 그래도 이해는 하고있는 점에서 위안을 받는다!!

내가 크롤링이라는 것을 하다니!!!

 

 

import requests
from bs4 import BeautifulSoup

headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
data = requests.get('https://www.genie.co.kr/chart/top200?ditc=M&rtm=N&ymd=20210701',headers=headers)

soup = BeautifulSoup(data.text, 'html.parser')


#body-content > div.newest-list > div > table > tbody > tr:nth-child(2)
#body-content > div.newest-list > div > table > tbody > tr:nth-child(3)
#body-content > div.newest-list > div > table > tbody > tr:nth-child(4)

trs = soup.select('#body-content > div.newest-list > div > table > tbody > tr')
# print(trs)
for tr in trs:
    rank = tr.select_one('td.number')
    # body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.info > a.title.ellipsis
    # body-content > div.newest-list > div > table > tbody > tr:nth-child(3) > td.info > a.title.ellipsis
    title = tr.select_one('td.info > a.title.ellipsis')
    # print(rank.text[0:2].strip())
    # print(rank.text[0:2].strip(),title.text.strip())

    # body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.info > a.artist.ellipsis
    artist = tr.select_one('td.info > a.artist.ellipsis')
    # print(rank.text[0:2].strip(),title.text.strip())
    print(rank.text[0:2].strip(),title.text.strip(),artist.text)


    # if a != None:
    # if rank is not None:
    #     # print(a.text)
    #     title = a.text
    #     rank = movie.select_one('td:nth-child(1) > img')['alt']
    #     star = movie.select_one('td.point').text
    #     # print(rank,title, star)
    #     doc = {
    #         'title':title,
    #         'rank':rank,
    #         'star':star
    #     }
# print(soup)

'파이썬' 카테고리의 다른 글

앱개발 플러스 2주차!  (0) 2022.05.22
앱개발 플러스 1주차!  (0) 2022.05.14
[D+23] 시간 참 빠르다  (0) 2022.04.28
[D+2] 개발일지: 아직도 뭐가뭔지 모르겠지만  (0) 2022.04.07
[D+1] 개발일지 첫 글  (0) 2022.04.06