• 카테고리

    질문 & 답변
  • 세부 분야

    풀스택

  • 해결 여부

    미해결

텔레그램 봇 만들기 파트에서 에러가 발생합니다.

23.01.03 09:02 작성 23.01.03 11:26 수정 조회수 844

0

안녕하세요, 강사님.

강의 잘 듣고 있습니다:)

현재 크롤링 후 텔레그램을 통해 메세지를 보내고, 장고에 연결하는 부분을 학습하고 있습니다.

강의 영상에서는 별도의 동기/비동기 처리 없이 진행이 가능했는데

그대로 따라하였더니 coroutine 'bot.sendMessage' was never awaited 에러가 발생하여

강의에서 작성한 for문을 async def test(): 로 함수로 감싼 뒤 스크립트 하단에

asyncio.run(test()) 함수를 실행시키니 에러가 발생하지 않고 텔레그램으로 메세지가 보내지더라구요.

그런데, 해당 스크립트를 django에 연결하기 위해선 def run 함수를 통해 진행해야하는데,

함수 형태가 조금 변경돼서 그런지 위와 같이 async, asyncio, await 등을 사용해봐도

에러가 발생하여 진행이 되지 않고있습니다..

어떻게 하면 좋을까요?? 코드 및 에러 첨부드립니다.

from asyncore import loop
from datetime import time
from bs4 import BeautifulSoup
import requests
import telegram
import asyncio
from hotdeal.models import Deal

async def test():
    response = requests.get('https://www.ppomppu.co.kr/zboard/zboard.php?id=ppomppu')

    soup = BeautifulSoup(response.text, "html.parser")
    BOT_TOKEN = 'TOKEN'

    bot = telegram.Bot(token=BOT_TOKEN)
    for item in soup.find_all("tr", {'class': ["list1", "list0"]}):
        
        try:
            image = item.find("img", class_="thumb_border").get("src")[2:]
            title = item.find("font", class_="list_title").text.strip()
            link = item.find("font", class_="list_title").parent.get("href")
            link = "https://www.ppomppu.co.kr/zboard/" + link
            reply_count = int(item.find("span", class_="list_comment2").text)
            up_count = item.find_all("td")[-2].text
            up_count = up_count.split("-")[0].strip()
            up_count = int(up_count)

            if up_count >= 5:
                if Deal.objects.filter(link__iexact=link).count() == 0:
                    Deal(image_url = image, title = title, link = link,
                    reply_count = reply_count, up_count = up_count).save()
                    await bot.sendMessage(-1001897599228, '{} {}'.format(title, link))

        except Exception as e:
            print(e)

def run():
    asyncio.run(test())

에러 메세지입니다.

invalid literal for int() with base 10: ''
'NoneType' object has no attribute 'text'
invalid literal for int() with base 10: ''
'NoneType' object has no attribute 'text'
invalid literal for int() with base 10: ''
'NoneType' object has no attribute 'text'
invalid literal for int() with base 10: ''
You cannot call this from an async context - use a thread or sync_to_async.
'NoneType' object has no attribute 'text'
invalid literal for int() with base 10: ''
'NoneType' object has no attribute 'text'
You cannot call this from an async context - use a thread or sync_to_async.
'NoneType' object has no attribute 'text'
You cannot call this from an async context - use a thread or sync_to_async.
You cannot call this from an async context - use a thread or sync_to_async.
You cannot call this from an async context - use a thread or sync_to_async.
You cannot call this from an async context - use a thread or sync_to_async.
You cannot call this from an async context - use a thread or sync_to_async.
You cannot call this from an async context - use a thread or sync_to_async.
'NoneType' object has no attribute 'text'
'NoneType' object has no attribute 'text'
invalid literal for int() with base 10: ''
'NoneType' object has no attribute 'text'
invalid literal for int() with base 10: ''

답변 0

답변을 작성해보세요.

답변을 기다리고 있는 질문이에요.
첫번째 답변을 남겨보세요!