Answer the question
In order to leave comments, you need to log in
How to output specific Python xml tags?
I have such a site.
I need to take the attribute title, wind_speed, etc. from it.
I wrote like this and it gave me all the attributes with text
from bs4 import BeautifulSoup
import requests
spicok = []
r = requests.get('https://export.yandex.ru/bar/reginfo.xml?region=76')
soup = BeautifulSoup(r.text, 'html.parser')
print(soup)
Answer the question
In order to leave comments, you need to log in
Install lxml if not installed:
--------------------------------
import requests
from bs4 import BeautifulSoup
spicok = []
r = requests.get('https://export.yandex.ru/bar/reginfo.xml?region=76')
soup = BeautifulSoup(r.text, 'lxml')
element = 'title'
find_element = soup.find(element)
# print(soup)
print(find_element.text)
import requests
from bs4 import BeautifulSoup
def get_spicok(*args):
spicok = []
for element in args:
try:
find_element = soup.find(element)
spicok.append(find_element.text)
except AttributeError:
print(f'Тег <{element}> не найден!')
return spicok
r = requests.get('https://export.yandex.ru/bar/reginfo.xml?region=76')
soup = BeautifulSoup(r.text, 'lxml')
spicok = get_spicok('title', 'wind_speed', 'и тд')
print(spicok) # Вывод:
# Тег <и тд> не найден!
# ['Хабаровск', '2']
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question