편집 기록

편집 기록
  • 프로필 nowp님의 편집
    날짜2020.02.24

    파이썬 list index out of range 에러


    import requests
    from bs4 import BeautifulSoup
    import os
    import time
    
    import telegram
    
    bot = telegram.Bot(token='123535:AASAAASDFDFDSFSD')
    chat_id = bot.getUpdates()[-1].message.chat.id
    
    
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    
    while True:
        req = requests.get('WebURL')
        req.encoding = 'euc-kr'
    
    
        soup = BeautifulSoup(req.content.decode('euc-kr','replace'))
        posts = soup.select('Select')
        latest = posts[0].text
    
    
    
        with open(os.path.join(BASE_DIR, 'a.txt'), 'r+') as f_read:
            before = f_read.readline()
            if before != latest:
                bot.sendMessage(chat_id=chat_id, text='TEXT')
    
    
        with open(os.path.join(BASE_DIR, 'a.txt'), 'w+') as f_write:
            f_write.write(latest)
            f_write.close()
    
        time.sleep(5) 
    

    급하게 크롤링 해야할 일이 생겨서 인터넷 검색으로 하드 코딩을 하는데 자꾸 파이썬에서 list index out of range 에러가 나옵니다. 어떻게 해야 할지 잘 모르겠습니다. 부탁드려요 조금 급합니다😢😢😢

    soup = BeautifulSoup(req.content.decode('euc-kr','replace'))
    Traceback (most recent call last):
    File "/root/a.py", line 23, in <module>
    latest = posts[0].text
    IndexError: list index out of range
    
  • 프로필 알 수 없는 사용자님의 편집
    날짜2020.02.24

    파이썬 list index out of range 에러


    import requests
    from bs4 import BeautifulSoup
    import os
    import time
    
    import telegram
    
    bot = telegram.Bot(token='123535:AASAAASDFDFDSFSD')
    chat_id = bot.getUpdates()[-1].message.chat.id
    
    
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    
    while True:
        req = requests.get('WebURL')
        req.encoding = 'euc-kr'
    
    
        soup = BeautifulSoup(req.content.decode('euc-kr','replace'))
        posts = soup.select('Select')
        latest = posts[0].text
    
    
    
        with open(os.path.join(BASE_DIR, 'a.txt'), 'r+') as f_read:
            before = f_read.readline()
            if before != latest:
                bot.sendMessage(chat_id=chat_id, text='TEXT')
    
    
        with open(os.path.join(BASE_DIR, 'a.txt'), 'w+') as f_write:
            f_write.write(latest)
            f_write.close()
    
        time.sleep(5) 
    

    급하게 크롤링 해야할 일이 생겨서 인터넷 검색으로 하드 코딩을 하는데 자꾸 파이썬에서 list index out of range 에러가 나옵니다. 어떻게 해야 할지 잘 모르겠습니다. 부탁드려요 조금 급합니다😢😢😢

    soup = BeautifulSoup(req.content.decode('euc-kr','replace'))
    Traceback (most recent call last):
    File "/root/a.py", line 23, in <module>
    latest = posts[0].text
    IndexError: list index out of range