Fjalarr Páll
Fjalarr Páll

Reputation: 23

Getting data from span class in python

I am new to data scraping and I am using BeautifulSoup to grap some data from a webpage.

What I have done is the following

all = soup.find_all("span",{"class":"compare-property"})

arg=all[0]

print(arg)

The output is:

< span class="compare-property" data-placement="top" data-propid="1858251"    data-toggle="tooltip" id="compare-link-1858251" title="Bera saman">
< i class="fa fa-plus"></i>
< /span>'

Now I need the number called data-propid, which is 1858251 in the example How can I get that number?

Upvotes: 2

Views: 1177

Answers (3)

Teghan Nightengale
Teghan Nightengale

Reputation: 528

Check out the beautiful soup documentation here: https://www.crummy.com/software/BeautifulSoup/bs4/doc/

You want:

for link in soup.find_all("span",{"class":"compare-property"}):
    print(link.get('data-propid'))

Upvotes: 1

Kai Dannies
Kai Dannies

Reputation: 56

You should get it with

all[0]['data-propid']

Greetings Kai Dannies

Upvotes: 1

facelessuser
facelessuser

Reputation: 1734

You can just access the attribute of the element.

all = soup.find_all("span",{"class":"compare-property"})

arg=all[0]

print(arg['data-propid'])

Upvotes: 1

Related Questions