A
A
alex444562021-01-25 04:55:59
Python
alex44456, 2021-01-25 04:55:59

How to limit BeautifulSoup's data parsing and collect only certain values?

The site has start and end times. I make a request for a page with information about pairs for a specific day, and display it to the user in a telegram bot using the aiogram library. The problem is that the information for each day is always under the same class attribute value. It's easier to show with an example.

Here information is only about the 3rd and 4th pair, because there are no others on this day:

<td><div>Пн</div><div class="mh-50 cell cell-vertical"><span class="lesson">3 пара</span><span class="start">11:45</span><span class="finish">13:20</span></div><div class="mh-50 cell cell-vertical"><span class="lesson">4 пара</span><span class="start">13:30</span><span class="finish">15:05</span></div></td>


And here is the information about the next day, there are already 4 pairs:
<td><div>Вт</div><div class="mh-50 cell cell-vertical"><span class="lesson">1 пара</span><span class="start">08:00</span><span class="finish">09:35</span></div><div class="mh-50 cell cell-vertical"><span class="lesson">2 пара</span><span class="start">09:45</span><span class="finish">11:20</span></div><div class="mh-50 cell cell-vertical"><span class="lesson">3 пара</span><span class="start">11:45</span><span class="finish">13:20</span></div><div class="mh-50 cell cell-vertical"><span class="lesson">4 пара</span><span class="start">13:30</span><span class="finish">15:05</span></div></td>


Problems arise due to the fact that on the page where information about the schedule for today is displayed, the standard time for other days is also displayed, although the items themselves are not shown:
xrkiu65h4w1clzrkvwihy-yvhzu.png

Here is the same but in the page code:
<td><div>Ср</div><div class="mh-50 cell cell-vertical"><span class="lesson">1 пара</span><span class="start">08:00</span><span class="finish">09:35</span></div></td>


Question: how can I collect information about the time of all couples only for a particular day? To parse the items themselves, I use the following code:
url = ''
    response = requests.get(url)
    html = response.text
    soup = BeautifulSoup(html, 'lxml')
    today = soup.findAll("div", {"class" : "cell mh-50", "data-rel" : "popover", "data-placement" : "right"})
    for i in range(0, len(today)):
      today_data = today[i]["data-content"].replace("<br>", "")
      await bot.send_message(message.from_user.id, today_data)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
datka, 2021-01-25
@datka

from datetime import date
today = date.today()
d1 = today.strftime("%d.%m.%Y")
print("d1 =", d1)

Further, if d1 = your date found by soup, then we display a couple.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question