xml синтаксический анализ для получения указанных c тегов - PullRequest
1 голос
/ 29 января 2020

У меня есть файл аннотации xml, в котором есть теги , я хочу найти тег для каждого действия и прочитать его значение (проверьте, является ли он Blur или нет) и для каждого действия я также хотите вернуть и . Как я могу это сделать? Есть ли инструментарий? Нужно ли читать каждый <тег> и находить всех его дочерних элементов?

<action>
    <temporal_region>
    <start_time>2683480</start_time>
    <stop_time>2684448</stop_time>
    </temporal_region>
    <action_type/>
    <state>1</state>
    <actuator>Incident</actuator>
    <description/><verb/><affected_list/><instrument_list/><recipient/>
    <origin>Blur</origin>
    <destination/>
    </action>

Редактировать:

Предложения, слегка расширенные для нескольких действий:

from bs4 import BeautifulSoup as bs

xml = """
<action>
<temporal_region>
<start_time>2683480</start_time>
<stop_time>2684448</stop_time>
</temporal_region>
<action_type/>
<state>1</state>
<actuator>Incident</actuator>
<description/><verb/><affected_list/><instrument_list/><recipient/>
<origin>Blur</origin>
<destination/>
</action>
<action>
<temporal_region>
<start_time>2683480</start_time>
<stop_time>2684448</stop_time>
</temporal_region>
<action_type/>
<state>1</state>
<actuator>Incident</actuator>
<description/><verb/><affected_list/><instrument_list/><recipient/>
<origin>Blur</origin>
<destination/>
</action>"""

soup = bs(xml, 'html.parser')
origin = soup.find('origin').text
print(len(origin))
start_time = soup.find('start_time').text
stop_time = soup.find('stop_time').text

if origin == 'Blur':
    print("success")

Возвращает 4, что, я полагаю, является открывающим и закрывающим тегами источника, в то время как у меня есть только 2 элемента.

Ответы [ 2 ]

1 голос
/ 29 января 2020

Другое решение.

from simplified_scrapy.simplified_doc import SimplifiedDoc

xml = """
<action>
<temporal_region>
<start_time>2683480</start_time>
<stop_time>2684448</stop_time>
</temporal_region>
<action_type/>
<state>1</state>
<actuator>Incident</actuator>
<description/><verb/><affected_list/><instrument_list/><recipient/>
<origin>Blur</origin>
<destination/>
</action>
<action>
<temporal_region>
<start_time>2683480</start_time>
<stop_time>2684448</stop_time>
</temporal_region>
<action_type/>
<state>1</state>
<actuator>Incident</actuator>
<description/><verb/><affected_list/><instrument_list/><recipient/>
<origin>Blur</origin>
<destination/>
</action>"""

doc = SimplifiedDoc(xml)
actions = doc.selects('action')
for action in actions:
  print (action.start_time)
  print (action.stop_time)
  print (action.origin)

Вот пример SimplifiedDo c: https://github.com/yiyedata/simplified-scrapy-demo/tree/master/doc_examples

1 голос
/ 29 января 2020

Вы можете использовать BeautifulSoup модуль из bs4 пакета

from bs4 import BeautifulSoup as bs

xml = """
<action>
<temporal_region>
<start_time>2683480</start_time>
<stop_time>2684448</stop_time>
</temporal_region>
<action_type/>
<state>1</state>
<actuator>Incident</actuator>
<description/><verb/><affected_list/><instrument_list/><recipient/>
<origin>Blur</origin>
<destination/>
</action>"""

soup = bs(xml, 'html.parser')
origin = soup.find('origin').text
start_time = soup.find('start_time').text
stop_time = soup.find('stop_time').text

if origin == 'Blur':
    print("success")
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...