如何刮擦嵌套的Div类

发布于 2025-02-10 13:43:46 字数 881 浏览 2 评论 0原文

嗨,我有一个本地HTML文件,其中包含聊天中的消息:

<div class="body">
<div class="pull_right date details" title="01.01.2022 01:01:01">
01:01
       </div>
<div class="from_name">
XYZ
       </div>
<div class="reply_to details">
In reply to <a href="#go_to_message23" onclick="return GoToMessage(747)">this message</a>
</div>
<div class="text">
Eat some chocolate
       </div>

现在我想创建一个DF,显示每条消息的某些信息。例如,我提取了用以下内容写消息的用户名称:

# doc.select('div[id]')[2].select_one('.from_name').text.strip()

messages = doc.select('div[id]')

for message in messages:
    print('---')
    try:
        print([message.select_one('.from_name').text.strip()])
    except:
        print("Couldn't find a name")

,但我不知道如何提取发送消息的日期。有人可以帮忙吗?谢谢

Hi, I have a local html file containing messages from a chat:

<div class="body">
<div class="pull_right date details" title="01.01.2022 01:01:01">
01:01
       </div>
<div class="from_name">
XYZ
       </div>
<div class="reply_to details">
In reply to <a href="#go_to_message23" onclick="return GoToMessage(747)">this message</a>
</div>
<div class="text">
Eat some chocolate
       </div>

Now I want to create a df showing certain information for each message. E.g I extracted the name of the user writing the message with:

# doc.select('div[id]')[2].select_one('.from_name').text.strip()

messages = doc.select('div[id]')

for message in messages:
    print('---')
    try:
        print([message.select_one('.from_name').text.strip()])
    except:
        print("Couldn't find a name")

But I can't figure out how to extract the date the message was sent. Can somebody help? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲歌长辞 2025-02-17 13:43:46

只需选择您的元素并调用其属性标题提取值:

select_one('div[title]').get('title')
示例
from bs4 import BeautifulSoup

html='''
<div class="body">
<div class="pull_right date details" title="01.01.2022 01:01:01">
01:01
       </div>
<div class="from_name">
XYZ
       </div>
<div class="reply_to details">
In reply to <a href="#go_to_message23" onclick="return GoToMessage(747)">this message</a>
</div>
<div class="text">
Eat some chocolate
       </div>'''

soup = BeautifulSoup(html)

data = []

for e in soup.select('div.body'):
    data.append({
        'date':e.select_one('div[title]').get('title'),
        'name':e.select_one('div.from_name').contents[0].strip(),
        'text':e.select_one('div.text').text.strip(),
    })
data
输出
 [{'date': '01.01.2022 01:01:01', 'name': 'XYZ', 'text': 'Eat some chocolate'}]

您可以简单地将结果转换为数据框:

import pandas as pd
pd.DataFrame(data)

输出

                  date name                text
0  01.01.2022 01:01:01  XYZ  Eat some chocolate

Simply select your element and call its attribute title to extract the value:

select_one('div[title]').get('title')
Example
from bs4 import BeautifulSoup

html='''
<div class="body">
<div class="pull_right date details" title="01.01.2022 01:01:01">
01:01
       </div>
<div class="from_name">
XYZ
       </div>
<div class="reply_to details">
In reply to <a href="#go_to_message23" onclick="return GoToMessage(747)">this message</a>
</div>
<div class="text">
Eat some chocolate
       </div>'''

soup = BeautifulSoup(html)

data = []

for e in soup.select('div.body'):
    data.append({
        'date':e.select_one('div[title]').get('title'),
        'name':e.select_one('div.from_name').contents[0].strip(),
        'text':e.select_one('div.text').text.strip(),
    })
data
Output
 [{'date': '01.01.2022 01:01:01', 'name': 'XYZ', 'text': 'Eat some chocolate'}]

You could simply turn your results into a dataframe:

import pandas as pd
pd.DataFrame(data)

Output

                  date name                text
0  01.01.2022 01:01:01  XYZ  Eat some chocolate
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文