BeautifulSoup抓取替代div

时间:2018-09-17 21:11:42

标签: python web-scraping beautifulsoup

我正在尝试抓取我作为学习实验编写的文件。看起来像这样:

<div class="container">
    <div class="date">1st</div>
    <div class="events">
        <div class="meeting">
            <span class="name">Bob</span>
        </div>
    </div>
    <div class="date">2nd</div>
    <div class="event">
        <div class="meeting">
            <span class="name">Emma</span>
            <span class="name">Frank</span>
            <span class="name">Charlie</span>
        </div>
    </div>
    <div class="date">3rd</div>
    <div class="event">
        <div class="meeting">
            <span class="name">Lisa</span>
            <span class="name">Tony</span>
        </div>
    </div>
</div>

我想抓取数据,以便它返回具有关联日期的Span。例如:

data = [['1st', 'bob'], ['2nd', 'Emma', 'Frank' 'Charlie'], ['3rd', 'Lisa', 'Tony']]

我遇到的问题是,当我使用以下内容进行抓取时,Div的dateevent处于同一级别:

for data in schedule_soup.find_all('div', 'container'):
    for date in data.find_all('div', 'date'):
        print(date)
    for name in data.find_all('span', 'name'):
        print(name)

我明白了:

<div class="date">1st</div>
<div class="date">2nd</div>
<div class="date">3rd</div>
<span class="name">Bob</span>
<span class="name">Emma</span>
<span class="name">Frank</span>
<span class="name">Charlie</span>
<span class="name">Lisa</span>
<span class="name">Tony</span>

2 个答案:

答案 0 :(得分:1)

尝试使用下面的代码,它对我有用

final_list=[]
dates = soup.find_all('div', 'date')
for c in range(len(dates)):
    temp_list=[]
    temp_list.append(dates[c].text)
    meeting = soup.find_all('div', 'meeting')
    meeting = BeautifulSoup(str(meeting[c]),'html.parser')
    for name in meeting.find_all('span','name'):
        temp_list.append(name.text)
    final_list.append(temp_list)
print(final_list)

输出

  

[[''stst','Bob'],['2nd','Emma','Frank','Charlie'],['3rd','Lisa','Tony']]

答案 1 :(得分:0)

您可以使用zip功能:

final_list=[]
dates = soup.find_all('div', 'date')
meetings = soup.find_all('div', 'meeting')
for date1, meeting in zip(dates, meetings):
    temp_list=[]
    temp_list.append(date1.text)
    [temp_list.append(x.text) for x in meeting.find_all('span')]
    final_list.append(temp_list)
print (final_list)