Почему не воспроизвоться блок кода aiogram
Даю участок кода, который не воспроизводиться при попытке сделать это.
@dp.message_handler(Text(equals='Ноутбуки', ignore_case=True), state=PriceState.parser)
@dp.message_handler(commands=['laptops'], state=PriceState.parser)
async def parsing_laptops(message: types.Message, state: FSMContext):
data = await state.get_data()
url = 'https://www.kufar.by/l/r~vitebsk/noutbuki'
params = {'prc': f'r%3A{data["min_price"]*100}%2C{data["max_price"]*100}', 'sort': 'lst.d'}
with requests.get(url, params=params) as r:
b = BeautifulSoup(r.text, 'html.parser')
i = 0
for section in b.find_all('section'):
price = str(section.span.text)
title = str(section.find('h3', {'class': 'styles_title__ARIVF'}).text)
city = str(section.find('div', {'class': 'styles_secondary__NEYhw'}).p.text)
url = str(section.find('a', {'class': 'styles_wrapper__yaLfq'})['href'])
url_photo = section.find('img', {'class': 'styles_image__eGgZr lazyload'})['data-src']
caption = f'Название: {title}\nГород: {city}\nЦена: {price}\nСсылка на объявление: {url}'
await message.answer_photo(photo=url_photo, caption=caption)
Прикладываю фото, как пытался вызвать команды
Ответы (1 шт):
Автор решения: Dendi
→ Ссылка
может это сработает?:
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters import Text
import requests
from bs4 import BeautifulSoup
bot = Bot(token='YOUR_TELEGRAM_TOKEN')
dp = Dispatcher(bot, storage=MemoryStorage())
@dp.message_handler(Text(equals='Ноутбуки', ignore_case=True), state=PriceState.parser)
@dp.message_handler(commands=['laptops'], state=PriceState.parser)
async def parsing_laptops(message: types.Message, state: FSMContext):
data = await state.get_data()
url = 'https://www.kufar.by/l/r~vitebsk/noutbuki'
params = {'prc': f'r%3A{data["min_price"]*100}%2C{data["max_price"]*100}', 'sort': 'lst.d'}
r = requests.get(url, params=params)
b = BeautifulSoup(r.text, 'html.parser')
i = 0
for section in b.find_all('section'):
price = str(section.span.text)
title = str(section.find('h3', {'class': 'styles_title__ARIVF'}).text)
city = str(section.find('div', {'class': 'styles_secondary__NEYhw'}).p.text)
url = str(section.find('a', {'class': 'styles_wrapper__yaLfq'})['href'])
url_photo = section.find('img', {'class': 'styles_image__eGgZr lazyload'})['data-src']
caption = f'Название: {title}\nГород: {city}\nЦена: {price}\nСсылка на объявление: {url}'
await message.answer_photo(photo=url_photo, caption=caption)
if __name__ == '__main__':
from aiogram import executor
executor.start_polling(dp)