python - 在其他 div 内的 div 之后抓取文本

标签 python html beautifulsoup

我有 html 代码:

    <h3 class="s-item__title s-item__title--has-tags" role="text">
        <div>
            <div class="s-item__title-tag">SOLD  Oct 9, 2018</div>
        </div>
        <span class="LIGHT_HIGHLIGHT">New Listing</span>MAX DELL PRECISION M4800 I7-4930XM EXTREME EDITION 3GHz 16GB 500GB SSD K2100 FHD
    </h3>

我想要购买“MAX DELL PRECISION M4800 I7-4930XM EXTREME EDITION 3GHz 16GB 500GB SSD K2100 FHD”,但是当我这样做时:

pre_title_and_sold=rel_text.findAll("h3",{"class":"s-item__title s-item__title--has-tags"})
print(pre_title_and_sold)

我明白

SOLD  Oct 8, 2018Dell Precision M6500 17" LED Workstation i7 1.6GHz 4GB RAM 111GB HD DVDRW Win 7

我想要得到

"MAX DELL PRECISION M4800 I7-4930XM EXTREME EDITION 3GHz 16GB 500GB SSD K2100 FHD"

我想如果我能够删除该部分,我可以执行 [0].text 路由(因为我正在迭代网页)

<div class="s-item__title-tag">SOLD  Oct 9, 2018</div>

我尝试查看文档,但很困惑。 我的问题有点类似于Get content after a div within another div但仍需要澄清。

Python 3.5 与 BeautifulSoup win 10

最佳答案

您必须找到包含该文本的实际标签。检查此代码。我提供了 2 个选项来查找文本!

from bs4 import BeautifulSoup
html = '''<h3 class="s-item__title s-item__title--has-tags" role="text">
        <div>
            <div class="s-item__title-tag">SOLD  Oct 9, 2018</div>
        </div>
        <span class="LIGHT_HIGHLIGHT">New Listing</span>MAX DELL PRECISION M4800 I7-4930XM EXTREME EDITION 3GHz 16GB 500GB SSD K2100 FHD
    </h3>'''

soup = BeautifulSoup(html, 'html.parser')
title = soup.find('h3', attrs={'s-item__title s-item__title--has-tags'}).span.next_sibling#first option
title1 = soup.select('.LIGHT_HIGHLIGHT')[0].next_sibling#second option
print(title)
print(title1)

输出:

MAX DELL PRECISION M4800 I7-4930XM EXTREME EDITION 3GHz 16GB 500GB SSD K2100 FHD

关于python - 在其他 div 内的 div 之后抓取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52747732/

相关文章:

python - 从python中的字符串消息中提取括号 '[ ]'中指定的数据

python - 为什么从 Paste 应用程序中创建 neo4j.GraphDatabase 会导致段错误?

javascript - 特定跨度的 Angular 切换颜色

php - 如何将具有动态大小的图像蒙版到形状?

loops - 如何使用 BeautifulSoup 成对提取表数据?

python - 如何防止 BeautifulSoup 转换实体?

python - Pyramid 复选框

php - jQuery 自动完成 - 隐藏 php 请求页面内容

python - 无法解析包含表格数据(iframe)的网站中的元素

python - 我正在尝试检测屏幕上的移动但收到错误 : ValueError: too many values to unpack (expected 4)