Relentless
Relentless

Reputation: 13

Get string from html element in python

<div aim="" base="1005260" class="item card cb23 eb02 rb3 d1" ele="2" imgur="kMBzbLb" quantity="" release="" res="1005260" type="1"><div class="content"></div><a class="ab" hash="" href="/cards/1005260-vegito-peerless-combination" title="Peerless Combination - Vegito"><table><tr><td class="dokkan"></td><td class="element"></td></tr><tr><td class="rarity"><i>ssr</i></td><td class="lock off"><i class="material-icons off"></i><i class="material-icons on"></i></td></tr></table></a><div class="dv">11921</div></div>

req = Request('https://dbz.space/summons/100', headers={'User-Agent': 'Mozilla/5.0'})

htmlbytes = urlopen(req).read()

htmlstrings = htmlbytes.decode("utf8")

parsed_html = BeautifulSoup(htmlstrings, features="html.parser")

first_card = parsed_html.find('div',attrs={"class" : "item card cb23 eb02 rb3 d1"})

print(str(first_card))

I'm trying to get the 1005260 out of the base "element" using this python script i made which gives me the whole block.

The print output would be: 1005260

Thanks for any help

Upvotes: 1

Views: 389

Answers (2)

Ramin Melikov
Ramin Melikov

Reputation: 1005

import re

pattern = r'base=\"(?P<groupname>[0-9]{1,10})\"'

s = '<div aim="" base="1005260" class="item card cb23 eb02 rb3 d1" ele="2" imgur="kMBzbLb" quantity="" release="" res="1005260" type="1"><div class="content"></div><a class="ab" hash="" href="/cards/1005260-vegito-peerless-combination" title="Peerless Combination - Vegito"><table><tr><td class="dokkan"></td><td class="element"></td></tr><tr><td class="rarity"><i>ssr</i></td><td class="lock off"><i class="material-icons off"></i><i class="material-icons on"></i></td></tr></table></a><div class="dv">11921</div></div>'

int(re.search(pattern, s).group('groupname'))

Upvotes: 0

dspencer
dspencer

Reputation: 4461

You can access the value of the attribute "base" of the div tag by using square-bracket indexing on first_card (treating it like a dictionary), as per the BeautifulSoup documentation:

print(first_card["base"])

Output:

1005260

Upvotes: 1

Related Questions