프로그래밍/파이썬

퀘사이존 타세요(임시) 게시글 크롤링 텔레그램 발송 봇

젤리씨 2022. 4. 20. 17:17
728x90

퀘사이존 타세요(임시) 게시글 크롤링 텔레그램 발송 봇을 만들어 봤습니다

 

요새 하루가 다르게 그래픽카드 가격이 내려가네여.ㅎ

 

import requests
from bs4 import BeautifulSoup
import os
import telegram

bot = telegram.Bot(token='텔레그램 토큰')
#chat_id = bot.getUpdates()[-1].message.chat.id
BASE_DIR = os.path.dirname(os.path.abspath(__file__))

#req = requests.get('https://www.ddengle.com/buyinfo')
req = requests.get('https://quasarzone.com/bbs/qb_tsy')
req.encoding = 'utf-8'

html = req.text
soup = BeautifulSoup(html, 'html.parser')
table = soup.find("table")
#print(table)
post = table.find("a", class_="subject-link")["href"]
#print(post)
posts = 'https://quasarzone.com'+ post
print(posts)

latest = posts

#ss = soup.find("a", class_="subject-link")["href"]

with open(os.path.join(BASE_DIR, 'quasar_zone.txt'), 'r+') as f_read:
    before = f_read.readline()
    if before != latest:
        bot.sendMessage(챗아이디, text='새 글이 올라왔어요!'+posts)
    #else: # 원래는 이 메시지를 보낼 필요가 없지만, 테스트 할 때는 봇이 동작하>$
        #bot.sendMessage(chat_id=chat_id, text='새 글이 없어요 ㅠㅠ')
    f_read.close()

with open(os.path.join(BASE_DIR, 'quasar_zone.txt'), 'w+') as f_write:
    f_write.write(latest)
    f_write.close()

 

내용중에 챗아이디 와 텔레그램 토큰만 잘 넣으시면 나옵니다.ㅎ

 

파이썬 파일과 동일한 폴더에 quasar_zone.txt 파일이 있어야 합니다.

 

그냥 비어있는 파일 하나 생성하시면 되요

728x90