0

I want to parse site with information about film sessions. For that I'm using parser BeautifulSoup but it returns incorrect data. For example, if I manually check it directly in code page, it has times at 27th: 23:45, 19:40. But it returns incorrect list ['21:00', '23:00'] and incorrect data from div:

<div class="showtimes-line has-21 has-23">
 <div class="showtimes-line-technology t-cinetech t-2d">
  <div class="showtimes-line-technology-title ">
   Cinetech+, 2D
  </div>
  <div class="showtimes-line-hours-wrapper">
   <a class="time h-21 " data-brand="Планета Кіно" data-category="2d" data-id="00000000000000000000000000000631" data-list="movie" data-name="Дедпул 2 (18+)" data-position="4" data-seat="" href="https://pay.planetakino.ua/hall/imax-kiev/484437" rel="nofollow">
    21:00
   </a>
   <a class="time h-23 " data-brand="Планета Кіно" data-category="2d" data-id="00000000000000000000000000000631" data-list="movie" data-name="Дедпул 2 (18+)" data-position="5" data-seat="" href="https://pay.planetakino.ua/hall/imax-kiev/486327" rel="nofollow">
    23:00
   </a>
  </div>
 </div>
</div>

Here is my code:

def get_sessions(response, date):
    """Return information about sessions at `date`."""
    sessions = []
    soup = BeautifulSoup(response.text, 'lxml')
    days_div = soup.find_all('div', class_='showtimes-row')

    for div in days_div:
        day_str = div.find('span', class_='date').text
        day_int = int(day_str.split()[0])

        if int(date.day) == day_int:
            sessions_row = div.find('div', class_='showtimes-line')
            for session in sessions_row.find_all('a', class_='time'):
                sessions.append(session.text.strip())
            print(sessions) 
            print(sessions_row.prettify())

    return sessions

The request was made as follows:

url='https://planetakino.ua/lvov2/movies/deadpool_2/#cinetech_2d_3d_4dx_week'
response = requests.get(url)
sessions = get_sessions(response, film.period)
12
  • could you possibly provide the requests that you are sending? Jun 25, 2018 at 17:23
  • @Fozoro yes, question was updated Jun 25, 2018 at 17:30
  • Great, thank you very much, I have two more questions for you, Do you just want the sessions of a given website? and what does film.period represent? Jun 25, 2018 at 17:34
  • @Fozoro I want a list of sessions at date which is specified at film.period (object of datetime). I compare days of dates here if int(date.day) == day_int. Jun 25, 2018 at 17:42
  • the code you gave is correct. the wrong data you see does not come from this page.
    – bobrobbob
    Jun 25, 2018 at 18:11

1 Answer 1

4

I haven't noticed that you provided film.period in the code hosted on Github, so I didn't bother to debug your code without that and decided to implement it from scratch.

After a quick search, I found out that the Planeta Kino cinema has XML files on their website with the showtimes of the movies. You can find some of them here. I'm not sure why, but there was no entry for lvov2 cinema with the showtimes corresponding to the link from your question. However, I managed to find it, by simply changing part of the URL: http://planetakino.ua/lvov2/ua/showtimes/xml/.

The code below should do exactly what you want:

import datetime
from typing import List

import dateparser
import requests
from bs4 import BeautifulSoup, Tag

Date = datetime.datetime
Screening = Tag
Screenings = List[Tag]


def get_movie_id(soup: BeautifulSoup, searched_movie: str) -> int:
    movie = soup.find(
        lambda elem: elem.name == 'movie' and searched_movie in elem.title.string
    )
    movie_id = int(movie['id'])
    return movie_id


def get_movie_screenings(soup: BeautifulSoup, movie_id: int, searched_date: Date) -> Screenings:
    formatted_date = searched_date.strftime('%Y-%m-%d')
    screenings = soup.select(f'showtimes '
                             f'> day[date={formatted_date}] '
                             f'> show[movie-id={movie_id}]')
    return screenings


def get_show_times(searched_movie: str, searched_date: Date) -> Screenings:
    url = 'http://planetakino.ua/lvov2/ua/showtimes/xml/'
    html = requests.get(url).text
    soup = BeautifulSoup(html, 'xml')

    movie_id = get_movie_id(soup, searched_movie)
    screenings = get_movie_screenings(soup, movie_id, searched_date)
    return screenings


date = dateparser.parse(input('Type the date: '))
if date is not None:
    import pprint
    pprint.pprint(get_show_times('Дедпул 2', date))
else:
    print('Sorry, I cannot parse the date you gave me.')

Output:

Type the date: 27 червня, середа
[<show full-date="2018-06-27 19:40:00" hall-id="104" movie-id="2385" order-url="https://pay.planetakino.ua/hall/pk-lvov2/485693" technology="Cinetech+2D" theatre-id="pk-lvov2" time="19:40"/>,
 <show full-date="2018-06-27 23:45:00" hall-id="101" movie-id="2385" order-url="https://pay.planetakino.ua/hall/pk-lvov2/485506" technology="4dx" theatre-id="pk-lvov2" time="23:45"/>]

I used dateparser to parse the input date, so it works with different formats, languages, e.g. 27th June, 27 червня, середа and a lot more. It's really great and I love it.

Take your time to read and understand the code, you might wanna look at .select() and CSS selectors (child combinator and attribute selectors).

Note: You need to use Python 3.6+, since I used Literal String Interpolation (f-strings) and type hinting (3.5+).

1
  • his code already does exactly what he wants: wasting your time
    – bobrobbob
    Jun 26, 2018 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.