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!!
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'
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!!