Python Forum
New to Python. How do i read out this number?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New to Python. How do i read out this number?
#1
Hello,

i am new to Python. For my first project i want to scrap the number "CY"... but i cant find a solution to scrap it.

Can you help me with the command to make python read the number? I need this figure as a variable to cotinue working with.

Thanks a lot in advance!!
Reply
#2
Can take a look at this.
from bs4 import BeautifulSoup

html = '''\
<g class="column column-0" id="dataPointLayerNr0">
<g class="dotGroup group-0" id="dotGroupNr0"><circle cx="137" cy="203.25" r="10.8" id="Gesamt" class="dot Gesamt" stroke="white" stroke-width="3" fill="#000000"></circle>
<circle cx="137" cy="203.25" r="43.2" id="GesamtFallback" class="dot fallback Gesamt" stroke="white" stroke-width="3" fill="#000000"></circle>
<text x="137" y="211.25" class="valText" text-anchor="middle" font-size="18" fill="#ffffff">67,35</text></g></g>'''

soup = BeautifulSoup(html, 'lxml')
Usage test:
>>> cy_val = soup.find('circle', class_="dot Gesamt")
>>> cy_val
<circle class="dot Gesamt" cx="137" cy="203.25" fill="#000000" id="Gesamt" r="10.8" stroke="white" stroke-width="3"></circle>
>>> cy_val.get('cx')
'137'
So attributes is a dictionary after found tag as shown over.
>>> cy = cy_val.attrs
>>> cy
{'class': ['dot', 'Gesamt'],
 'cx': '137',
 'cy': '203.25',
 'fill': '#000000',
 'id': 'Gesamt',
 'r': '10.8',
 'stroke': 'white',
 'stroke-width': '3'}
>>> 
>>> list(cy.items())[1]
('cy', '203.25')
>>> list(cy.items())[1][0]
'cy'
>>> list(cy.items())[1][1]
'203.25' 
dhl047 likes this post
Reply
#3
Thanks a lot, snippsat!!

already helped me much.

I am now at the point where i can print this: {'cx': '137', 'cy': '203.25', 'r': '10.8', 'id': 'Gesamt', 'class': ['dot', 'Gesamt'], 'stroke': 'white', 'stroke-width': '3', 'fill': '#000000'}

This is my current code.
html = '''\
<g class="column column-0" id="dataPointLayerNr0">
<g class="dotGroup group-0" id="dotGroupNr0"><circle cx="137" cy="203.25" r="10.8" id="Gesamt" class="dot Gesamt" stroke="white" stroke-width="3" fill="#000000"></circle>
<circle cx="137" cy="203.25" r="43.2" id="GesamtFallback" class="dot fallback Gesamt" stroke="white" stroke-width="3" fill="#000000"></circle>
<text x="137" y="211.25" class="valText" text-anchor="middle" font-size="18" fill="#ffffff">67,35</text></g></g>'''

soup = BeautifulSoup(html, 'lxml')

cy_val = soup.find('circle', class_="dot Gesamt")

cy_val = soup.find('circle', class_="dot Gesamt")
cy_val

cy_val.get('cx')
'137'

cy = cy_val.attrs

list(cy.items())

print(cy)
I only need the value of CY to go further and make a lot of of if conditions.
Bit embarrassed but i am new to python and trying to figure this out for some time.

Thanks a lot again!!
Larz60+ write Mar-25-2021, 07:29 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use bbcode tags on future posts
Reply


Forum Jump:

User Panel Messages

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