• 로그인
  • 장바구니에 상품이 없습니다.

home2 게시판 Python, SQL 게시판 네이버 주식창 웹크롤링 질문입니다.

네이버 주식창 웹크롤링 질문입니다.

4 글 보임 - 1 에서 4 까지 (총 4 중에서)
  • 글쓴이
  • #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'
    
    #41398

    codingapple
    키 마스터
    soup.select() 오타인듯요 
    결과 뒤에 .strip() 붙여봅시다 
    
    #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
    
    어떤 문제가 있는지 알 수 없어 질문합니다. 답변해주시 감사하겠습니다 !
    
    
    주식 크롤링
    #107939

    codingapple
    키 마스터
    requests.get()안에 현재 웹페이지 url 집어넣어서 해봅시다
4 글 보임 - 1 에서 4 까지 (총 4 중에서)
  • 답변은 로그인 후 가능합니다.

About

현재 월 700명 신규수강중입니다.

  (09:00~20:00) 빠른 상담은 카톡 플러스친구 코딩애플 (링크)
  admin@codingapple.com
  이용약관
ⓒ Codingapple, 강의 예제, 영상 복제 금지
top

© Codingapple, All rights reserved. 슈퍼로켓 에듀케이션 / 서울특별시 강동구 고덕로 19길 30 / 사업자등록번호 : 212-26-14752 온라인 교육학원업 / 통신판매업신고번호 : 제 2017-서울강동-0002 호 / 개인정보관리자 : 박종흠