Python Forum
[SOLVED] [BeautifulSoup] Why attribute not found?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] [BeautifulSoup] Why attribute not found?
#1
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")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [BeautifulSoup] Why does it turn inserted string's brackets into &lt;/&gt;? Winfried 0 1,587 Sep-03-2022, 11:21 PM
Last Post: Winfried
  [SOLVED] [Beautifulsoup] Find if element exists, and edit/append? Winfried 2 4,454 Sep-03-2022, 10:14 PM
Last Post: Winfried
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 1,164 Aug-19-2022, 08:07 PM
Last Post: Winfried
  [SOLVED] [BeautifulSoup] How to get this text? Winfried 6 2,073 Aug-17-2022, 03:58 PM
Last Post: Winfried
  [SOLVED] Tkinter module not found Milan 7 27,485 Aug-05-2022, 09:45 PM
Last Post: woooee
  No Module found in other directory than source code [SOLVED] AlphaInc 1 2,129 Nov-10-2021, 04:34 PM
Last Post: AlphaInc
  How can I found how many numbers are there in a Collatz Sequence that I found? cananb 2 2,599 Nov-23-2020, 05:15 PM
Last Post: cananb

Forum Jump:

User Panel Messages

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