如何使用beautifulsoup从以下HTML代码中提取数据?

时间:2016-02-07 07:53:35

标签: python html python-3.x beautifulsoup

我想使用BeautifulSouppython3的“电影”和“时间”类从div中提取数据。我怎样才能使用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>

这是我的代码:

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

2 个答案:

答案 0 :(得分:1)

您可以在findAll中指定这两个类:

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

答案 1 :(得分:0)

&#34; div&#34;你感兴趣的是另一个&#34; div&#34;儿童。为了得到这个&#34; div&#34;您可以使用.select方法。

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))

或者迭代find_all()结果并使用.find()方法返回&#34; div&#34;其中class: "timings"

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