2016-02-07 103 views
1

我想使用BeautifulSouppython3来从div'cinema'和'timing'中提取数据。我怎样才能使用soup.findAll如何使用beautifulsoup从以下HTML代码中提取数据?

<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 
+0

在soup.findAll DIV( '格',{ '类': '电影' }): –

+0

print div.text 它没有打印任何东西,程序刚刚结束 –

回答

1

可以在findAll指定两类:

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

的 “格”,你感兴趣的是另一种 “格” 的孩子。要获得该“div”,您可以使用.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()方法返回那些 “格” 里class: "timings"

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