0

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']})

2 Answers2

0
test = [r['data-ds-appid'] for r in soup.find_all(name="a", attrs={"data-ds-appid":True})]
Cobra
  • 200
  • 9
  • Thank you that worked however what is that r for? – Ghassan Shahzad Jul 03 '19 at 14:59
  • In my example, `r` represents an `a` tag and thanks to the comprehension list, you can get all `data-ds-appid` of the page if of course you get all the html code in the soup. :) – Cobra Jul 03 '19 at 15:02
0

Use css selector and attribute data-ds-appid to fetch the value.

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'])

Output:

1046930
KunduK
  • 32,888
  • 5
  • 17
  • 41