Sarthak Agarwal
Sarthak Agarwal

Reputation: 11

How to extract data form the below HTML code using beautifulsoup?

I want to extract data from the div with class 'cinema' and 'timings' using BeautifulSoup in python3 . How can i do it using soup.findAll ?

<div data-order="0" class="cinema">
<div class="__name">SRS Shoppers Pride Mall<span class="__venue">&nbsp;-&nbsp; Bijnor</span>
</div>
<div class="timings"><span class="__time _available" onclick="fnPushWzKmEvent('SRBI',ShowData);fnCallSeatLayout('SRBI','22876','ET00015438','01:30 PM');">01:30 PM</span><span class="__time _center _available" onclick="fnPushWzKmEvent('SRBI',ShowData);fnCallSeatLayout('SRBI','22877','ET00015438','04:00 PM');">04:00 PM</span><span class="__time _right _available" onclick="fnPushWzKmEvent('SRBI',ShowData);fnCallSeatLayout('SRBI','22878','ET00015438','06:30 PM');">06:30 PM</span><span class="__time _available" onclick="fnPushWzKmEvent('SRBI',ShowData);fnCallSeatLayout('SRBI','22879','ET00015438','09:00 PM');">09:00 PM</span>
</div>
</div>

This is my code:

for div in soup.findAll('div',{'class':'cinema'}):
    print div.text # It printed nothing ,the program just ended 

Upvotes: 1

Views: 109

Answers (2)

Sede
Sede

Reputation: 61225

The "div" you are interested in is another "div" child. To get that "div" you can use the .select method.

from bs4 import BeautifulSoup

html = <your html>
soup = BeautifulSoup(html, 'lxml')
for div in soup.select('div.cinema > div.timings'):
    print(div.get_text(strip=True))

Or iterate the find_all() result and use the .find() method to return those "div" where class: "timings"

for div in soup.find_all('div', class_='cinema'):
    timings = div.find('div', class_='timings')
    print(timings.get_text(strip=True))

Upvotes: 0

midori
midori

Reputation: 4837

You can specify both classes in findAll:

soup.findAll(True, {'class': ['cinema', 'timings']})

Upvotes: 1

Related Questions