1 回答
TA贡献1876条经验 获得超5个赞
用于findAll
提取符合给定条件的 Tag 对象列表,然后zip
并行迭代公开迭代。
from bs4 import BeautifulSoup
input_ = """<section id="content4" class="tab-content">
<p>
<div class="Text_Title">Product 1</div>
<div style="display: inline-block;">Red Ball<div></p>
<p>
<div class="Text_Title">Product 2</div>
<div style="display: inline-block;">Green Ball</div></p>
<p>
<div class="Text_Title">Product 3</div>
<div style="display: inline-block;">Yellow Ball</div></p>"""
soup = BeautifulSoup(input_, "html.parser")
for x, y in zip(soup.findAll("div", attrs={"class": "Text_Title"}),
soup.findAll("div", attrs={"style": "display: inline-block;"})):
print(x.text, "-", y.text)
Product 1 - Red Ball
Product 2 - Green Ball
Product 3 - Yellow Ball
添加回答
举报