Search code examples
pythonweb-scrapingtimebeautifulsoupdatetime-select

Problem: how to get a list of tag attribute values with beautifulsoup


I want to scrape from the website of a local cinema and get all times, when a certain film is running.

I found the table in which the times are listed in the following form:

[<time datetime="2020-01-31T21:15:00+01:00">21:15</time>].

If I use beautifulsoup as x.find_all('time'). I get all the times in the form shown above. But I only want the 'datetime' so in this case 2020-01-31T21: 15: 00 + 01: 00. Now when I search for x.find_all('time')['datetime'] I get the following error:

TypeError: list indices must be integers or slices, not str

Can someone tell me how I use beautifulsoup to create a list in which all 'datetime' are collected?

thanks in advance


Solution

  • x.find_all('time')
    

    will return a list. So you'll have to get an item from the list before you can get the "datetime" attribute.

    x.find_all('time')[0]['datetime']
    

    will probably do it.