使用BS4提取属性值

时间:2019-07-03 14:52:46

标签: python html beautifulsoup

所以我试图使用bs4提取attrib的值,但无法使它起作用。

<a href="https://store.steampowered.com/app/1046930/Dota_Underlords/?snr=1_7_7_230_150_1"  data-ds-appid="1046930" data-ds-tagids="[493,113,493,9,1708,14139,3859]" data-ds-crtrids="[4]" onmouseover="GameHover( this, event, 'global_hover', {&quot;type&quot;:&quot;app&quot;,&quot;id&quot;:1046930,&quot;v6&quot;:1} );" onmouseout="HideGameHover( this, event, 'global_hover' )" class="search_result_row ds_collapse_flag " >

多数民众赞成在很长的标签,以便生病只是将其压缩到我需要的部分

<a href="https://store.steampowered.com/app/1046930/Dota_Underlords/?snr=1_7_7_230_150_1"  data-ds-appid="1046930"

从缩短的代码中,我需要data-ds-appid及其值,而无需知道该值的开头 我的代码:

stuff = soup.find("a", {"data-ds-appid":['content']})

2 个答案:

答案 0 :(得分:0)

test = [r['data-ds-appid'] for r in soup.find_all(name="a", attrs={"data-ds-appid":True})]

答案 1 :(得分:0)

使用css selector和属性data-ds-appid来获取值。

from bs4 import BeautifulSoup
data='''<a href="https://store.steampowered.com/app/1046930/Dota_Underlords/?snr=1_7_7_230_150_1"  data-ds-appid="1046930" data-ds-tagids="[493,113,493,9,1708,14139,3859]" data-ds-crtrids="[4]" onmouseover="GameHover( this, event, 'global_hover', {&quot;type&quot;:&quot;app&quot;,&quot;id&quot;:1046930,&quot;v6&quot;:1} );" onmouseout="HideGameHover( this, event, 'global_hover' )" class="search_result_row ds_collapse_flag " >'''
soup=BeautifulSoup(data,'html.parser')
print(soup.select_one('a[data-ds-appid]')['data-ds-appid'])

输出:

1046930