python - 使用 BeautifulSoup 提取特定的嵌套 div

标签 python beautifulsoup

我有这段 HTML 代码,我正在为其创建脚本: http://imgur.com/a/dPNYI
我想提取突出显示的文本(“一些文本”)并打印出来。

我尝试遍历每个嵌套的 div 以找到我需要的 div,如下所示:

import requests
from bs4 import BeautifulSoup

url = "the url this is from"
r = requests.get(url)

for div in soup.find_all("div", {"id": "main"}):
    for div2 in div.find_all("div", {"id": "app"}):
        for div3 in div2.find_all("div", {"id": "right-sidebar"}):
            for div4 in div3.find_all("div", {"id": "chat"}):
                for div5 in div4.find_all("div", {"id": "chat-messages"}):
                    for div6 in div5.find_all("div", {"class": "chat-message"}):
                        for div7 in div6.find_all("div", {"class": "chat-message-content selectable"}):
                            print(div7.text.strip())

我实现了我在指南和在线类似问题中看到的内容,但我敢打赌这还差得远,一定有更简单的方法。
这行不通。它不打印任何东西,我有点迷路了。如何打印突出显示的行(这实际上是 div 的第一个 div 子元素,其 id 为“chat-messages”)?

HTML 代码:

<!DOCTYPE html>

<html>
<head>
    <title>
    </title>
</head>

<body>
    <div id="main">
        <div data-reactroot="" id="app">
            <div class="top-bar-authenticated" id="top-bar">
            </div>


            <div class="closed" id="navigation-bar">
            </div>


            <div id="right-sidebar">
                <div id="chat">
                    <div id="chat-head">
                    </div>


                    <div id="chat-title">
                    </div>


                    <div id="chat-messages">
                        <div class="chat-message">
                            <div class="chat-message-avatar" style="background-image: url(&quot;https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/65/657dcec97cc00bc378629930ecae1776c0d981e0.jpg&quot;);">
                            </div>
                            <a class="chat-message-username clickable">
                            <div class="iron-color">
                                aloe
                            </div></a>

                            <div class="chat-message-content selectable">
                                <!-- react-text: 2532 -->some text<!-- /react-text -->
                            </div>
                        </div>


                        <div class="chat-message">
                        </div>


                        <div class="chat-message">
                        </div>


                        <div class="chat-message">
                        </div>


                        <div class="chat-message">
                        </div>


                        <div class="chat-message">
                        </div>

最佳答案

使用 lxml 解析器(即 soup = BeautifulSoup(data, 'lxml')),您可以将 .find 与多个类一起使用,就像查找嵌套 div 就像单个类一样简单:

soup.find('div',{'class':'chat-message-content selectable'}).text

只要该类的出现是 html 中的唯一一个,上面的行就应该适合您。

关于python - 使用 BeautifulSoup 提取特定的嵌套 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45601680/

相关文章:

python - 如何识别并跟踪链接,然后使用 BeautifulSoup 从新网页打印数据

python - 如何在 web.py 中上传图片并将其保存到磁盘

python时间一试除了

python - BeautifulSoup:find_all() 和 unicode 的问题?

python - 如何在 python 列表中编码 bs4 可导航字符串?

Python,获取嵌入视频网址时遇到问题

python - Python:计算X射线中两 block 骨头之间的角度

python - Pandas groupby() 比较并计算两列

java - Java 中 RequestConfig 的 Python 等价物是什么?

python - 如何使用 Python 从 HTML 获取 href 链接?