簡體   English   中英

Python:搜索字符串,在該字符串之后解析文本,然后添加到列表中

[英]Python: Search for a string, parse text after that string, add to a list

因此,我有大量的XML(例如,這是https://www.goodreads.com/author/list/20598?format=xml&key=pVrw9BAFGMTuvfj4Y8VHQ ),並且我想針對字符串的每個外觀進行搜索<title>,然后解析其后的文本以獲取實際標題,並將其臨時分配為變量的值,然后追加將該變量添加到列表中。

換句話說,瀏覽此XML並附帶一個列表標題。

然后我的問題(在搜索中,我看到了很多類似的東西,但完全不同):

1-如何遍歷整個文本,在每次出現的<title>處停止執行我在此描述的操作?

2-我應該如何准確解析該標題? 也就是說,我想捕獲在<title>和</ title>之間出現的字符串?

先發制人的感謝。

假設用<title>表示標題標簽 ,那么任何中途的XML解析器都可以輕松地做到這一點:它將在找到title標簽時通知您,然后提取該標簽中的文本(所需標題) 。

眾所周知,XML有很多解析器。 但是,如果您想自己執行此操作,則此功能將起作用,除了在注釋掉的文本中出現title元素標志(我不知道它們在技術上叫什么)的情況或非法的情況下文字部分。

def extract_text_between_flags(inputText, flagBegin, flagEnd):
    # Instantiate an empty list to store the results
    excerpts = list()

    # Find the first occurrence of the begin flag
    indexBegin = inputText.find(flagBegin)
    # Until the begin flag is no longer found
    while indexBegin > -1:
        # From the current begin flag location, search forward to the first
        # occurrence of the end flag
        indexEnd = inputText.find(flagEnd, indexBegin + len(flagBegin)) + len(flagEnd)
        # If the end flag is not found, stop searching
        if indexEnd <= 0:
            break
        # Extract the relevant passage from the text and add it to the list
        excerpt = inputText[indexBegin+len(flagBegin):indexEnd-len(flagEnd)]
        excerpts.append(excerpt)

        # Set the new search starting point as the next occurrence of the
        # begin flag
        indexBegin = inputText.find(flagBegin, indexEnd)

    return excerpts

titles = extract_text_between_flags(myXMLString, '< title >', '< /title >')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM