第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

我想找到一個(gè) <span> 標(biāo)簽,它位于包含多個(gè) <span> 標(biāo)簽的 <h1> 標(biāo)簽內(nèi)

我想找到一個(gè) <span> 標(biāo)簽,它位于包含多個(gè) <span> 標(biāo)簽的 <h1> 標(biāo)簽內(nèi)

阿晨1998 2022-06-22 18:12:29
我想要做的是選擇第二個(gè)跨度并抓住它的文本來(lái)打印它。下面是 HTML 代碼和 BeautifulSoup 代碼#HTML code<h1 id="productTitle">   <a href="https://www.example.com/product/">       <span id="productBrand">BRAND</span>   </a>   <span>PRODUCT TITLE </span></h1>#BeautifulSoup codefor h1 in soup.find_all('h1', id="productTitle"):    productTitle = h1.find('span').text    print(productTitle)
查看完整描述

2 回答

?
LEATH

TA貢獻(xiàn)1936條經(jīng)驗(yàn) 獲得超7個(gè)贊

希望,并非總是如此,id 應(yīng)該是唯一的含義find_all可能不是必需的。


使用 bs4 4.7.1+,您可以使用 :not 排除具有 id 的子跨度


from bs4 import BeautifulSoup as bs


html = '''<h1 id="productTitle">

   <a href="https://www.example.com/product/">

         <span id="productBrand">BRAND</span>

   </a>

         <span>PRODUCT TITLE </span>

</h1>

'''

soup = bs(html, 'lxml')

print(soup.select_one('#productTitle span:not([id])').text)

你也可以第n個(gè)孩子


print(soup.select_one('#productTitle span:nth-child(2)').text)

或者


print(soup.select_one('#productTitle span:nth-child(even)').text)

甚至是一個(gè)直接的兄弟姐妹組合來(lái)獲得span孩子a


print(soup.select_one('#productTitle a + span').text)

或鏈接 next_sibling


print(soup.select_one('#productTitle a').next_sibling.next_sibling.text)


查看完整回答
反對(duì) 回復(fù) 2022-06-22
?
素胚勾勒不出你

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超9個(gè)贊

h1這會(huì)在標(biāo)簽中獲取您需要的所有字段:


蟒蛇代碼:


from bs4 import BeautifulSoup

text = '''

<h1 id="productTitle">

   <a href="https://www.example.com/product/">

         <span id="productBrand">BRAND</span>

   </a>

         <span>PRODUCT TITLE </span>

</h1>

'''

soup = BeautifulSoup(text,features='html.parser')

#BeautifulSoup code


for h1 in soup.find_all('h1', id="productTitle"):

    spans = h1.find_all('span')

    print('productBrand  == > {}'.format(spans[0].text))

    print('productTitle  == > {}'.format(spans[1].text))

使用 h1 獲取所有跨度:


for h1 in soup.find_all('h1', id="productTitle"):

    for i,span in enumerate(h1.find_all('span')):

      print('span {} == > {}'.format(i,span.text))


查看完整回答
反對(duì) 回復(fù) 2022-06-22
  • 2 回答
  • 0 關(guān)注
  • 161 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)