BeautifulSoup刮交替div

问题描述 投票:1回答:2

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

<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>
python web-scraping beautifulsoup
2个回答
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)

Output

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


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)
© www.soinside.com 2019 - 2024. All rights reserved.