A
A
Artyom2021-03-28 17:54:18
Python
Artyom, 2021-03-28 17:54:18

How to take objects from xml?

import requests
from xml.etree import ElementTree

response = requests.get("https://steamcommunity.com/id/little4wingeneral/?xml=1")

tree = ElementTree.fromstring(response.content)

Let's say I have deduced all objects from xml ... And how can I deduce a certain object? For example: under the category I need to display the value of this object (76561198975722860). How to do it?
<steamID64>76561198975722860</steamID64>
<profile>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Karbivnichy, 2021-03-28
@artemgoruchev

Working with xml is another task. I would recommend you to use BeautifulSoup:

import requests
from bs4 import BeautifulSoup

response = requests.get("https://steamcommunity.com/id/little4wingeneral/?xml=1")

soup = BeautifulSoup(response.text,"html.parser")

steamID64 = soup.select_one('steamID64').text

print(steamID64)

>>> 76561198975722860
Accessing nested elements:
soup = BeautifulSoup(response.text,"html.parser")

groups = soup.select('group groupID64')

for group in groups:
  groupID64 = group.text
  print(groupID64)

103582791429670253
103582791436397384
103582791457672580
103582791461046683

Beautiful Soup CSS Selectors Cheat Sheet

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question