Warm tip: This article is reproduced from stackoverflow.com, please click
html python beautifulsoup

Using BS4 to extract a attrib value

发布于 2020-03-27 10:25:50

so i was trying to extract the value of an attrib using bs4 but couldnt get it to work.

<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 " >

thats the tag pretty long so ill just condense it to the part i need

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

from that shortened code i need the data-ds-appid and its value without knowing the value to begin with my code:

stuff = soup.find("a", {"data-ds-appid":['content']})
Questioner
Fr06t
Viewed
29
Cobra 2019-07-03 22:57
test = [r['data-ds-appid'] for r in soup.find_all(name="a", attrs={"data-ds-appid":True})]