Reputation: 623
header = head.find_all('span')
[<span itemprop="name">Raj</span>, <span itemprop="street">24 Omni Street</span>, <span itemprop="address">Ohio</span>, <span itemprop="Region">US</span>, <span itemprop="postal">40232</span>, <span class="number">334646344</span>]
print (header[0].tag)
print(header[0].text)
####output
None
Raj
...
####Expected output
Name
Raj
...
I could not able to extract all the value of span itemprop. It throws me None output. Am I doing something wrong?
Thanks, Raj
Upvotes: 1
Views: 582
Reputation: 8047
Yes, class 'bs4.element.Tag'
does not have a tag
attribute, as itself is a Tag
. From the docs:
You can access a tag’s attributes by treating the tag like a dictionary.
So you've got the list of all the span
tags, now just iterate the list and get their attribute that you want (i.e. 'itemprop'
):
spans = head.find_all('span')
for span in spans:
try:
print(span['itemprop'].decode().title() + ': ' + span.text)
except KeyError:
continue
output:
Name: Raj
Street: 24 Omni Street
Address: Ohio
Region: US
Postal: 40232
Format the output or store the data as needed
Upvotes: 2