Как я могу получить какое-то значение из XML int Python?

1

У меня есть этот файл Sitemap в xml. Как я могу получить каждый <loc>?

<?xml version="1.0" encoding="UTF-8"?>
<urlset
      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<!-- created with Free Online Sitemap Generator www.xml-sitemaps.com -->


<url>
  <loc>https://www.nsnam.org/wiki/Main_Page</loc>
  <lastmod>2018-10-24T03:03:05+00:00</lastmod>
  <priority>1.00</priority>
</url>
<url>
  <loc>https://www.nsnam.org/wiki/Current_Development</loc>
  <lastmod>2018-10-24T03:03:05+00:00</lastmod>
  <priority>0.80</priority>
</url>
<url>
  <loc>https://www.nsnam.org/wiki/Developer_FAQ</loc>
  <lastmod>2018-10-24T03:03:05+00:00</lastmod>
  <priority>0.80</priority>
</url>

Программа выглядит так.

import os.path
import xml.etree.ElementTree
import requests
from subprocess import call

def creatingListOfBrokenLinks():
    if (os.path.isfile('sitemap.xml')):
        e = xml.etree.ElementTree.parse('sitemap.xml').getroot()
        file = open("all_broken_links.txt", "w")

        for atype in e.findall('url'):
            r = requests.get(atype.find('loc').text)
            print(atype)
            if (r.status_code == 404):
                file.write(atype)

        file.close()


if __name__ == "__main__":
    creatingListOfBrokenLinks()
  • 0
    Как выглядит ваша Python-программа на данный момент?
  • 0
    @frankenapps я обновил
Теги:
elementtree

2 ответа

0
Лучший ответ

Я предлагаю вам использовать стандартный пакет библиотеки elementtree:

from xml.etree import ElementTree as ET

SITEMAP = """<?xml version="1.0" encoding="UTF-8"?>
<urlset
      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
    <!-- created with Free Online Sitemap Generator www.xml-sitemaps.com -->
    ...
    ...
</urlset>"""

urlset = ET.fromstring(SITEMAP)
loc_elements = urlset.iter("{http://www.sitemaps.org/schemas/sitemap/0.9}loc")
for loc_element in loc_elements:
    print(loc_element.text)

Ссылки на документацию:

Обновить:

  • То, что ваш код ошибается, - это обработка пространства имен XML.
  • Кроме того, мой пример использует .iter() вместо .findall()/.find(), чтобы получить loc элементы непосредственно. Это может быть или не быть нормально в зависимости от структуры XML и варианта использования.
0

Ваш код работал отлично на моем конце. Все, что вам нужно было сделать, это добавить: {http://www.sitemaps.org/schemas/sitemap/0.9} перед url и loc

Вот:

import os.path
import xml.etree.ElementTree
import requests
from subprocess import call

def creatingListOfBrokenLinks():
    if (os.path.isfile('sitemap.xml')):
        e = xml.etree.ElementTree.parse('sitemap.xml').getroot()
        file = open("all_broken_links.txt", "w")

        for atype in e.findall('{http://www.sitemaps.org/schemas/sitemap/0.9}url'):
            r = requests.get(atype.find('{http://www.sitemaps.org/schemas/sitemap/0.9}loc').text)
            print(atype)
            if (r.status_code == 404):
                file.write(atype)

        file.close()


if __name__ == "__main__":
    creatingListOfBrokenLinks()

Ещё вопросы

Сообщество Overcoder
Наверх
Меню