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

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

BeautifulSoup 中的自定義屬性?

BeautifulSoup 中的自定義屬性?

九州編程 2021-12-29 10:43:29
我正在嘗試使用美麗的湯來定位具有非標準屬性的 DIV。這是DIV:`<div data-asin="099655596X" data-index="1" class="sg-col-20-of-24 s-result-item sg-col-0-of-12 sg-col-28-of-32 sg-col-16-of-20 sg-col sg-col-32-of-36 sg-col-12-of-16 sg-col-24-of-28" data-cel widget="search_result_1">`我需要 find_all DIV 與 data-asin 屬性,并獲得 asin 。BS 似乎支持此功能,但我所做的不起作用。這是我不起作用的代碼:`rows = soup.find_all(attrs={"data-asin": "value"})`我需要如何在 Python3.7 中制作我的 BS 才能找到所有這些 DIV?
查看完整描述

1 回答

?
幕布斯7119047

TA貢獻1794條經驗 獲得超8個贊

使用 CSS 選擇器來實現。


from bs4 import BeautifulSoup

html = '''

<div data-asin="099655596X" data-index="1" class="sg-col-20-of-24 s-result-item sg-col-0-of-12 sg-col-28-of-32 sg-col-16-of-20 sg-col sg-col-32-of-36 sg-col-12-of-16 sg-col-24-of-28" data-cel widget="search_result_1">

'''

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

items=soup.select('div[data-asin="099655596X"]')

for item in items:

    print(item['data-asin'])

輸出:


099655596X

或者


from bs4 import BeautifulSoup

html = '''

<div data-asin="099655596X" data-index="1" class="sg-col-20-of-24 s-result-item sg-col-0-of-12 sg-col-28-of-32 sg-col-16-of-20 sg-col sg-col-32-of-36 sg-col-12-of-16 sg-col-24-of-28" data-cel widget="search_result_1">

'''

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

items=soup.select('div[data-asin$="X"]')

for item in items:

    print(item['data-asin'])


查看完整回答
反對 回復 2021-12-29
  • 1 回答
  • 0 關注
  • 485 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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