인프런 커뮤니티 질문&답변
django 스크립트에 비동기 함수를 사용하려면 어떻게 해야 할까요??
작성
·
562
0
안녕하세요? 장고 입문자입니다!
현재 웹 크롤링 후 특정 정보를 텔레그램 봇을 통해 전송하는 코드를 구현하고 있는데 갑작스레 비동기 함수를 사용해야 한다는 에러가 발생하더라구요.. (function was never awaited)
비동기 함수를 사용해본 적이 없다보니 구글링을 통해 1회성으로 작동하는 스크립트는 완성하였는데, 장고 프로젝트 내의 스크립트로 옮길 때도 다른 문제가 발생하더라구요..
from asyncore import loop
from datetime import time
from bs4 import BeautifulSoup
import requests
import telegram
import asyncio
from hotdeal.models import Deal # 특정 웹에 대해 크롤링한 객체(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())위 코드를 장고 내에서 작동시키기 위해 runscript를 사용할 경우 에러가 발생합니다ㅠ
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: ''async, asyncio, await, time.sleep() 등등 이것저것 사용해봤는데 마땅히 해결책을 찾기가 어렵네용.. 조언주시면 감사하겠습니다ㅠ
답변
답변을 기다리고 있는 질문이에요
첫번째 답변을 남겨보세요!




