I'm making a Telegram bot with Python that tells me the movie schedule. I'm leaving a question because I don't know how to show you the ticket.

Asked 2 years ago, Updated 2 years ago, 125 views

//import telegram
from telegram.ext import Updater
from telegram.ext import MessageHandler, Filters
from bs4 import BeautifulSoup
import urllib.request as req



######Yeongdeungpo CGv crawling related function######
code = req.urlopen("http://www.cgv.co.kr/common/showtimes/iframeTheater.aspx?areacode=01&theatercode=0059&date=20200315")
soup = BeautifulSoup(code, "html.parser")
movies = soup.select('body > div > div.sect-showtimes > ul > li')
for movie in movies:
     title = movie.select_one('div > div.info-movie > a > strong').get_text().strip()
timetables = movie.select('div > div.type-hall > div.info-timetable > ul > li')

def Movie_title_crawling():
    output_result = ""
    for movie in movies:
        title = movie.select_one('div > div.info-movie > a > strong').get_text().strip()
        output_result += title + "\n"
    return output_result

def Movie_timetable_crawling():
    tuples =[]
    for timetable in timetables:
        time = timetable.select_one('em').get_text()
        seat = timetable.select_one('span').get_text()
        tuple = (time,seat)
        tuples.append(tuple)
        for movie in movies:
            output_result = ""
            title = movie.select_one('div > div.info-movie > a > strong').get_text().strip()
            timetable = tuples
            output_result += title + timetable
    return output_result
############################

######Telegram related code#####
token = ""
id = ""
bot = telegram.Bot(token)
info_message = ''- Movie showing today: Enter "Movie" or "OHH"
- Today's movie screening schedule: "Show" or "S.O."
bot.sendMessage(chat_id = id, text=info_message)

# # updater
updater = Updater(token=token, use_context=True)
dispatcher = updater.dispatcher
updater.start_polling()


### Chatbot Reply
def handler(update,context):
    user_text = update.message.text #Save messages sent by users in user_text

    #Tell me about the movie you're showing
    if (user_text == "movie") or (user_text == "okh"):
        Movie = Movie_title_crawling()
        bot.send_message(chat_id = id, text="Today's movie\n{}".format(Movie))
        bot.sendMessage(chat_id=id, text=info_message)
    #Tell us today's screening schedule
    elif (user_text == "show") or (user_text == "s")"):
        Timetable=Movie_timetable_crawling()
        bot.send_message(chat_id = id, text="{}".format(Timetable))
        bot.sendMessage(chat_id=id, text=info_message)
echo_handler = MessageHandler(Filters.text, handler)
dispatcher.add_handler(echo_handler)

If I run this code, the movie comes out well, but I want to make the movie schedule look like the title of the movie, but I get an error and it doesn't work. TypeError: can only concatenate str (not "list") to str This error appears. I think we need to change the def Movie_timetable_crawling() function, but I'm not sure how to change it. I'd appreciate your help.

python telegram anaconda chatbot beautifulsoup

2022-09-20 15:59

1 Answers

>>> s = ""
>>> t = []
>>> s += t
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    s += t
TypeError: can only concatenate str (not "list") to str

Error when trying to add list to str.


2022-09-20 15:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.