2016-02-07 105 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)) 
相關問題