亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Python/BeautifulSoup 從多個 DIVS + DIV 樣式中提取文本

使用 Python/BeautifulSoup 從多個 DIVS + DIV 樣式中提取文本

一只斗牛犬 2023-05-09 14:50:16
我已經嘗試了多種解決方案來解決這個問題,但無法弄清楚我有以下 HTML:<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>    我試圖從DIV = Text_Title和中提取文本STYLE = display: inline-block;我試圖獲得的輸出:Product 1 - Red BallProduct 2 - Green BallProduct 3 - Yellow Ball
查看完整描述

1 回答

?
慕運維8079593

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


查看完整回答
反對 回復 2023-05-09
  • 1 回答
  • 0 關注
  • 161 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號