
https://spartacodingclub.kr/community/fastqna/web/62efd205b104434b7a1884b0/%EC%A0%9C%20%EC%BD%94%EB%93%9C%EB%A5%BC%20%EC%96%B4%EB%96%BB%EA%B2%8C%20%EA%B3%A0%EC%B3%90%EC%95%BC%20%ED%95%A0%EA%B9%8C%EC%9
고쳐보려는 시도를 계속하고 있으나 해결에 어려움을 겪고 있습니다.
제가 건드릴 때 마다 새로운 오류가 나오고 있네요 ㅎㅎ
이번 기회에 error 수집가가 되어볼까 합니다 ㅠㅜ
TypeError: 'NoneType' object is not subscriptable
SyntaxError: invalid syntax
IndentationError: unexpected indent
KeyError: 'alt'
도전1.
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']
point = soup.select_one('#old_content > table > tbody > tr')
for c_point in point :
c = c_point.select_one('td.point')
if a is not None:
title = a.text
rank = b
star = c
print(rank,title,star)
#old_content > table > tbody > tr:nth-child(2) > td:nth-child(1) > img
#old_content > table > tbody > tr:nth-child(2) > td.point
도전1.오류
Traceback (most recent call last):
File "C:/Users/super/OneDrive/Desktop/sparta/pythonprac/scraping ver0.1.py", line 23, in <module>
b = b_alt.select_one('td:nth-child(1) > img')['alt']
TypeError: 'NoneType' object is not subscriptable
종료 코드 1(으)로 완료된 프로세스
도전2.
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']
point = soup.select_one('#old_content > table > tbody > tr')
for c_point in point :
c = c_point.select_one('td.point')
if a is not None:
title = a.text
rank = b
star = c
print(rank,title,star)
#old_content > table > tbody > tr:nth-child(2) > td:nth-child(1) > img
#old_content > table > tbody > tr:nth-child(2) > td.point
도전2.오류
File "C:/Users/super/OneDrive/Desktop/sparta/pythonprac/scraping ver0.1.py", line 23
b = b_alt?.select_one('td:nth-child(1) > img')['alt']
^
SyntaxError: invalid syntax
종료 코드 1(으)로 완료된 프로세스
도전3 부터는 b에 집중하기 위해서 c를 제거 해서 진행했습니다.
도전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.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.오류
File "C:/Users/super/OneDrive/Desktop/sparta/pythonprac/scraping ver0.1 serious .py", line 28
if a is not None:
^
IndentationError: unexpected indent
종료 코드 1(으)로 완료된 프로세스
도전4.
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')['alt']
if a is not None:
title = a.text
rank = b
print(rank,title)
이번 에러입니다.
도전4.오류
Traceback (most recent call last):
File "C:/Users/super/OneDrive/Desktop/sparta/pythonprac/scraping ver0.1 serious .py", line 19, in <module>
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'
참고 중인 사항
https://stackoverflow.com/questions/58102668/webscraping-typeerror-nonetype-object-is-not-subscriptable
https://www.reddit.com/r/learnpython/comments/88e33b/web_scraping_typeerror_nonetype_object_is_not/
