Python Forum
[SOLVED] [BeautifulSoup] Why do I get a KeyError?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] [BeautifulSoup] Why do I get a KeyError?
#1
Question 
Hello,

For some reason, while this line works fine when using a local string in a test script, I get a "KeyError" when actually fetching a web page although I can see the element with that attribute does exist in the page source:

#KeyError: 'data-share-icon'
if soup.span['data-share-icon']:
Any idea what it could be?

Thank you.
--
Edit: Using this shortcut (I didn't know) triggers the same error:
if soup['data-share-icon']:
--
Edit: No better
if soup['data-share-icon'] is not None:
--
Edit: I can see that attribute and its value in the dump as expected :-/
reqs = requests.get(url, headers=HEADERS)
soup = BeautifulSoup(reqs.text, 'lxml')
#to investigate
with open("soup.txt", "w", encoding="utf-8") as text_file:
	text_file.write(str(soup))
--
Edit: This triggers the exception:

from bs4 import BeautifulSoup
import json

KEY = "data-share-icon"
html = '''<span data-share-icon='{"title":"Blah","locale":"en-US"}'></span>'''

soup = BeautifulSoup(html, 'lxml')
print(soup)

try:
	data = soup[KEY]
except:
	print("Bad")
--
Edit: Couild it be that BS doesn't support dashes in key names?
KEY = "data-share-icon"
html = '''<head><title>MyTitle</title></head><span data-share-icon='{"title":"Blah","locale":"en-US"}'></span>'''
KEY = "mykey"
html = '''<head><title>MyTitle</title></head><span mykey='{"title":"Blah","locale":"en-US"}'></span>'''

soup = BeautifulSoup(html, 'lxml')
if soup.span[KEY]:
	print("OK")
--
Edit: Apparently, some kung-fu is required when the attribute contains dashes/hyphens… and to get its value instead of the whole HTML string
if soup.select_one(f"span[{KEY}]"):
	#returns whole tree, not attribute value!
	#data = soup.select_one(f"span[{KEY}]")
	data = soup.span[KEY]
	print(data)
	data = json.loads(data)
	print("Found title:",data['title'])
Reply
#2
from bs4 import BeautifulSoup

html = '''<span data-share-icon='{"title":"Blah","locale":"en-US"}'></span>'''
soup = BeautifulSoup(html, 'lxml')
>>> KEY = soup.span.attrs # Or soup.find('span')
>>> data = KEY['data-share-icon']
>>> data
'{"title":"Blah","locale":"en-US"}'
>>> # Or
>>> soup.select_one('span')['data-share-icon']
'{"title":"Blah","locale":"en-US"}'
Reply
#3
Thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] [BeautifulSoup] Adding newline before+after new element? Winfried 5 107 Feb-25-2026, 09:01 AM
Last Post: Winfried
Question [SOLVED] [BeautifulSoup] A few questions Winfried 2 589 Nov-20-2025, 02:10 PM
Last Post: Winfried
  [Solved]Help with BeautifulSoup.getText() Error Extra 5 8,056 Jan-19-2023, 02:03 PM
Last Post: prvncpa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020