Извлечение подстроки из элемента XML - PullRequest
0 голосов
/ 23 сентября 2019

Я пытаюсь извлечь подстроку «Abstract» из элементов «Description» XML-канала RSS.Фрагмент кода:

import feedparser

rss = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/erss.cgi?rss_guid=1RGmO3jHeXUu8o2CWPinET6JLLik93hwR2IAJ5mU-YzoPeX1-O'
feed = feedparser.parse(rss)

for post in feed.entries:
   print (post.description)

Строка, которую я хочу получить от первого элемента, встроена в описание между <p>Abstract<br/> ...... <br/>:

The recent development of electronic logbooks with secure off-device data storage provides a rich resource for research. We present the largest analysis of anaesthetic logbooks to date, with data from 494,235 cases logged by 964 anaesthetists over a 4-year period. Our analysis describes and compares the annual case-load and supervision levels of different grades of anaesthetists across the UK and Republic of Ireland. We calculated the number of cases undertaken per year by grade (median (IQR [range]) core trainees = 388 (252-512 [52-1204]); specialist trainees = 344 (228-480 [52-1144]); and consultants = 328 (204-500 [52-1316]). Overall, the proportion of cases undertaken with direct consultant supervision was 56.7% and 41.6% for core trainees and specialist trainees, respectively. The proportion of supervised cases reduced out-of-hours, for both core trainees (day 93.5%, evening 86.3%, night 78.6%) and specialist trainees (day 81.0%, evening 67.7%, night 56.4%)

Я не уверен, как отделить все остальное отописание отдельно от аннотации.Я предполагаю, что использую поиск регулярных выражений, я пробовал ответы на похожие вопросы о подстроке и не смог заставить его работать, возможно, из-за тегов html.

Очень ценил

1 Ответ

0 голосов
/ 28 сентября 2019

Для анализа результатов фида PubMed вы можете использовать BeautifulSoup .

from bs4 import BeautifulSoup

# find all p(aragraph) tags which have "Abstract" in their text
def find_abstract(tag):
    return tag.name == 'p' and tag.text.startswith('Abstract')

soup = BeautifulSoup(post.description)

# find the abstract
result = soup.find(find_abstract)

# format the text: keep everything but the first line
abstract = '\n'.join(result.text.splitlines()[1:]).strip()
print(abstract)
...