P
P
Ping Wins2019-02-23 14:00:27
Python
Ping Wins, 2019-02-23 14:00:27

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

1 answer(s)
B
BJlaDuMup, 2019-02-23
@BJlaDuMup

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)

Finished implementation:
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 question

Ask a Question

731 491 924 answers to any question