4 글 보임 - 1 에서 4 까지 (총 4 중에서)
-
글쓴이글
-
2022년 8월 8일 15:12 #41336
김남규참가자안녕하세요. 1. '웹크롤러 2 - 혼자 코드짤 때 도움되는 case study' 강의 부분의 class, id가 없는 요소 찾기에서 네이버 주식창 '동일업종 등락률'을 찾는 방법으로 마우스 우클릭 -> copy -> copy selecor해서 soup.select('')에 넣어 찾으니 에러 납니다. NoneType 오브젝트는 callable 하지 않다고 나오는데 원인이 무엇일가요?
<소스 코드> import requests from bs4 import BeautifulSoup 데이터 = requests.get('https://finance.naver.com/item/sise.nhn?code=005930') soup = BeautifulSoup(데이터.content, 'html.parser') soup.selector('#tab_con1 > div:nth-child(6) > table > tbody > tr:nth-child(2) > td > em') <실행 결과>
TypeError Traceback (most recent call last) <ipython-input-60-f09c916f0766> in <module> 5 6 soup = BeautifulSoup(데이터.content, 'html.parser') ----> 7 soup.selector('#tab_con1 > div:nth-child(6) > table > tbody > tr:nth-child(2) > td > em')
TypeError: 'NoneType' object is not callable 2. 위와 비슷한 내용인데 soup.select('.gray .f_down em')[0]까지 하면 잘 출력되는데 soup.select('.gray .f_down em')[0].text하면 목표한 데이터가 아니라 줄바꿈 명령 등과 같이 함께 나오는데 어떻게 하면 될까요? <소스코드>
import requests from bs4 import BeautifulSoup 데이터 = requests.get('https://finance.naver.com/item/sise.nhn?code=005930') soup = BeautifulSoup(데이터.content, 'html.parser') soup.select('.gray .f_down em')[0] soup.select('.gray .f_down em')[0].text < 출력결과 : soup.select('.gray .f_down em')[0]> -1.16%
< 출력결과 : soup.select('.gray .f_down em')[0].text > '\n\t\t\t\t-1.27%\n\t\t\t\t'
2023년 12월 26일 22:31 #107900
이경희참가자동일업종 등락률 관련해서 여쭤볼게 있습니다. 저의 html 창에서는 해당 부분 클래스가 "f_up"이어서 이 부분은 제 것에 맞게 바꿔서 코드를 작성했는데요, 알려주신 두 방법 모두 제대로 작동을 하지 않아 질문합니다. 1) soup.select('.gray .f_up em')[0].text 해당 내용을 작성 후 출력하면 아무것도 뜨지 않습니다. 2) soup.select('#tab_con1 > div:nth-child(6) > table > tbody > tr:nth-child(2) > td > em')[0].text or .text.strip() 해당 부분은 .text / .text.strip() 두 가지 경우 같은 에러가 뜹니다. 에러는 아래와 같습니다. Traceback (most recent call last): File "c:\PyhtonWorkspace\naver\app.py", line 17, in <module> soup.select('.gray .f_down em')[0].text ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^ IndexError: list index out of range 어떤 문제가 있는지 알 수 없어 질문합니다. 답변해주시 감사하겠습니다 !
-
글쓴이글
4 글 보임 - 1 에서 4 까지 (총 4 중에서)
- 답변은 로그인 후 가능합니다.