커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
답변 완료
3-8 웹스크래핑 2가지 오류로 줄어 들었습니다. 하지만 핵심을 파악하지 못했습니다. <4번째 재질문>
undefined주차
북마크
탁*현
댓글
6
추천
0
조회수
41
조회수
41
답변 완료

일단 제가 문법이 틀려서 생긴 오류들을 제외하고 세 개의 오류, 두 가지 종류의 오류가 남았습니다.

그리고 실행 되어서 오류가 뜨지는 않았지만 작동이 되지 않는 경우도 있었습니다.


TypeError: 'NoneType'

  b = b_alt.select_one('td:nth-child(1) > img')['alt']
TypeError: 'NoneType' object is not subscriptable


  b = b_alt.img.select_one('td:nth-child(1) > img')['alt']
AttributeError: 'NoneType' object has no attribute 'select_one'


KeyError: 'alt'


    b_rank = soup.select_one('#old_content > table > tbody > tr')['alt']
  File "C:\Users\super\OneDrive\Desktop\sparta\pythonprac\venv\lib\site-packages\bs4\element.py", line 1519, in __getitem__
    return self.attrs[key]
KeyError: 'alt'



제가 만약에 이 문제를 해결하지 못한다면 다음에 웹스크래핑을 할 때

ranking 데이터 등 img 데이터를 처리하지 못할 것 같습니다.

스파르타 즉문즉답

1.도전



실행창

    b = b_alt.select_one('td:nth-child(1) > img')['alt']
TypeError: 'NoneType' object is not subscriptable


전체 코드

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.naver?sel=pnt&date=20210829',headers=headers)

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

#old_content > table > tbody > tr:nth-child(3) > td.title > div > a



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

for movie in movies :

    a = movie.select_one('td.title>div > a')

b_rank = soup.select_one('#old_content > table > tbody > tr')

for b_alt in b_rank :

    b = b_alt.select_one('td:nth-child(1) > img')['alt']


    if a is not None:
        title = a.text
        rank = b

        print(rank,title)


2.도전



실행창


 b = b_alt.img.select_one('td:nth-child(1) > img')['alt']
AttributeError: 'NoneType' object has no attribute 'select_one'

전체 코드

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.naver?sel=pnt&date=20210829',headers=headers)

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

#old_content > table > tbody > tr:nth-child(3) > td.title > div > a



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

for movie in movies :

    a = movie.select_one('td.title>div > a')

b_rank = soup.select_one('#old_content > table > tbody > tr')

for b_alt in b_rank :

    b = b_alt.img.select_one('td:nth-child(1) > img')['alt']

    point = soup.select_one('#old_content > table > tbody > tr')


    if a is not None:
        title = a.text
        rank = b

        print(rank,title)

3.도전



실행창

 b_rank = soup.select_one('#old_content > table > tbody > tr')['alt']
  File "C:\Users\super\OneDrive\Desktop\sparta\pythonprac\venv\lib\site-packages\bs4\element.py", line 1519, in __getitem__
    return self.attrs[key]
KeyError: 'alt


전체 코드

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.naver?sel=pnt&date=20210829',headers=headers)

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

#old_content > table > tbody > tr:nth-child(3) > td.title > div > a



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

for movie in movies :

    a = movie.select_one('td.title>div > a')

b_rank = soup.select_one('#old_content > table > tbody > tr')['alt']

for b_alt in b_rank :

    b = b_alt.select_one('td:nth-child(1) > img')


    if a is not None:
        title = a.text
        rank = b

        print(rank,title)


4.도전


실행창

종료 코드 0(으)로 완료된 프로세스

전체코드

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.naver?sel=pnt&date=20210829',headers=headers)

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

#old_content > table > tbody > tr:nth-child(3) > td.title > div > a



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

for movie in movies :

    a = movie.select_one('td.title>div > a')

b_rank = soup.select_one('#old_content > table > tbody > tr')

for b_alt in b_rank :

    b = b_alt.select_one('td:nth-child(1) > img')


    if a is not None:
        title = a.text
        rank = b

        print(rank,title)



취소
 공유
취소
댓글 0
댓글 알림
나의얼굴