Python Forum
[SOLVED] [BeautifulSoup] Why attribute not found? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: [SOLVED] [BeautifulSoup] Why attribute not found? (/thread-39574.html)



[SOLVED] [BeautifulSoup] Why attribute not found? - Winfried - Mar-11-2023

Hello,

I can't figure out why BS can't find the (first) element that holds the "latitude" attribute:

for item in glob.glob("index.*"):
	soup = BeautifulSoup(open(item, 'r',encoding='utf-8'), "lxml")

	#OK
	url = soup.find("link",{"rel":"canonical"}).get("href")
	if url:
		print(url)
	else:
		print("No URL")
		
	"""
	<div
	id="blah"
	latitude="1.23"
	longitude="4.56"
	>
	"""
	lat = soup.find("div").get("latitude")
	lat = soup.find("div","latitude") #no better
	if lat:
		print("Found lat=",lat)
	else:
		print("No lat")
Is there something obvious I missed?

Thank you.
---
Edit: It works if I use the following to find the element:

lat = soup.find("div",{"id":"99"}).get("latitude")
if lat:
	print("Found lat=",lat)
else:
	print("No lat")