파이썬 크롤링

Jo_oJ·2022년 3월 15일

스파르타코딩클럽

목록 보기
9/12
post-thumbnail

으아아
또 해보라고 하는거 하다가 막혀서 일주일은 쉬었다...........ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ
이제 11일? 10일? 밖에 안남았는데 아직도 3주차 중반,, 후,,,,, 가보자고

내가한거

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://movie.naver.com/movie/sdb/rank/rmovie.nhn?sel=pnt&date=20200303',headers=headers)

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

trs = soup.select('#old_content > table > tbody > tr')

#old_content > table > tbody > tr:nth-child(2) > td.title > div > a
#old_content > table > tbody > tr:nth-child(2) > td.point
#old_content > table > tbody > tr:nth-child(9) > td:nth-child(1) > img

for tr in trs:
    a_tag = tr.select_one('td.title > div > a')
    b_tag = tr.select_one('td > img')
    c_tag = tr.select_one('td.point')
    if b_tag is not None:
        title = a_tag.text
        rank = b_tag
        star = c_tag.text
        print(rank,title,star)

결과값

<img alt="41" height="13" src="https://ssl.pstatic.net/imgmovie/2007/img/common/bullet_r_g41.gif" width="14"/> 굿바이 마이 프랜드 9.35
<img alt="42" height="13" src="https://ssl.pstatic.net/imgmovie/2007/img/common/bullet_r_g42.gif" width="14"/> 서유기 2 - 선리기연 9.35
<img alt="43" height="13" src="https://ssl.pstatic.net/imgmovie/2007/img/common/bullet_r_g43.gif" width="14"/> 주토피아 9.35
<img alt="44" height="13" src="https://ssl.pstatic.net/imgmovie/2007/img/common/bullet_r_g44.gif" width="14"/> 두 교황 9.35
<img alt="45" height="13" src="https://ssl.pstatic.net/imgmovie/2007/img/common/bullet_r_g45.gif" width="14"/> 굿 윌 헌팅 9.35
<img alt="46" height="13" src="https://ssl.pstatic.net/imgmovie/2007/img/common/bullet_r_g46.gif" width="14"/> 클레멘타인 9.35
<img alt="47" height="13" src="https://ssl.pstatic.net/imgmovie/2007/img/common/bullet_r_g47.gif" width="14"/> 히든 피겨스 9.35
<img alt="48" height="13" src="https://ssl.pstatic.net/imgmovie/2007/img/common/bullet_r_g48.gif" width="14"/> 세 얼간이 9.35
<img alt="49" height="13" src="https://ssl.pstatic.net/imgmovie/2007/img/common/bullet_r_g49.gif" width="14"/> 쉰들러 리스트 9.34
<img alt="50" height="13" src="https://ssl.pstatic.net/imgmovie/2007/img/common/bullet_r_g50.gif" width="14"/> 울지마 톤즈 9.34

이ㅏ씨 랭크값을 ㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠ
rank = b_tag(['alt']) 라고 해도 염병천병 안되길래 건드리다가 포기했는데
rank['alt'] 였다 ㅎㅎ = rank = b_tag['alt']
ㅎ 낄낄............................................................
그놈의 b_tag.text 고정관념에 사로잡혀서,,,,,, . .. . . 후...

요건 정답?

for tr in trs:
    a_tag = tr.select_one('td.title > div > a')
    if a_tag is not None:
        rank = tr.select_one('td:nth-child(1) > img')['alt']
        title = a_tag.text
        star = tr.select_one('td.point').text
        print(rank,title,star)

나는 a,b,c로 나누니까 if 문 적을 때 뭘 넣어야 하는지 모르겠어서 애매했는데
이렇게 보니까 깔꼼ㅡ하다......................
의미없는 문단 늘리기보다는 합치는게 중요하륻ㅅ
정의가 제대로 정립되지 않으니까 계속 해체하는데..... 진짜.. 음....... 음.....
난 아직 배우는 단계기 땜이 괜찬아..

0개의 댓글