@Cat_usual

Как парсить XML?

Помогите, пожалуйста.

url = 'https://vk.com/foaf.php?id=' + str(user_id)
    test = requests.get(url).text
    with open("data.xml", 'w') as f:
        f.write(str(test))

    tree = ElementTree.parse('data.xml')
    root = tree.getroot()
    #print(root)
    print('Имя: ' + root[0][4].text)
    print('Фамилия: ' + root[0][5].text)
    print('Дата регистрации: ' + str(root[0][9]))


Выполняется данный код, вот код из data.xml

<?xml version="1.0" encoding="WINDOWS-1251"?>
<rdf:RDF
    xml:lang="ru"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:foaf="http://xmlns.com/foaf/0.1/"
    xmlns:ya="http://blogs.yandex.ru/schema/foaf/"
    xmlns:img="http://blogs.yandex.ru/schema/foaf/"
    xmlns:dc="http://purl.org/dc/elements/1.1/">
  <foaf:Person>
    <ya:publicAccess>allowed</ya:publicAccess>
    <ya:profileState>active</ya:profileState>
    <ya:URI ya:primary="yes" rdf:resource="http://vk.com/id554205532"/>
    <ya:URI rdf:resource="http://vk.com/peter.parkers"/>
    <ya:firstName>Peter</ya:firstName>
    <ya:secondName>Parker</ya:secondName>
    <foaf:name>Peter Parker</foaf:name>
    <foaf:weblog
        dc:title=""
        rdf:resource="http://vk.com/peter.parkers"/>
    <foaf:gender>male</foaf:gender>
    <ya:created dc:date="2019-07-21T22:17:38+03:00"/>
    <ya:lastLoggedIn dc:date="2019-07-25T20:15:19+03:00"/>
    <ya:modified dc:date="2019-07-21T22:26:00+03:00"/>
    <ya:subscribersCount>0</ya:subscribersCount>
    <ya:friendsCount>18</ya:friendsCount>
    <ya:subscribedToCount>0</ya:subscribedToCount>
    <foaf:birthday>08-10</foaf:birthday>
    <foaf:dateOfBirth>2001-08-10</foaf:dateOfBirth>
    <foaf:img>
      <foaf:Image ya:primary="yes" img:width="200" img:height="200"
          rdf:about="https://sun7-9.userapi.com/c855224/v855224038/9f700/s_S9w2H4kpo.jpg?ava=1">
        <foaf:thumbnail img:width="100" img:height="100" rdf:resource="https://sun7-9.userapi.com/c855224/v855224038/9f701/4nHJR2bAkgI.jpg?ava=1"/>
        <foaf:thumbnail img:width="50" img:height="50" rdf:resource="https://sun7-6.userapi.com/c855224/v855224038/9f702/8Wv_gSkGHP8.jpg?ava=1"/>
      </foaf:Image>
    </foaf:img>
    <foaf:familyStatus>inlove</foaf:familyStatus>
    <ya:job>
      <ya:workPlace
          dc:title=""
          rdf:about="http://yandex.ru/yandsearch?text=%2C%20">
        
      </ya:workPlace>
    </ya:job>
  </foaf:Person>
</rdf:RDF>


Как получить строку , и преобразовать её в нормальное время?
  • Вопрос задан
  • 589 просмотров
Решения вопроса 1
NeiroNx
@NeiroNx
Программист
print('Дата регистрации: ' + str(datetime.strptime(root[0][9].attrib['{http://purl.org/dc/elements/1.1/}date'],'%Y-%m-%dT%H:%M:%S%z').date().strftime('%d.%m.%Y')))
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы