简体   繁体   中英

Print Text between two words in a text file multitime using python

This is the text in my text file.

<a>
Some Text 1.....
</a>
Some Other Text
<a>
Some Text 2.....
</a>
Some Other Text
<a>
Some Text 3.....
</a>

I need to extract string between a tags and write every string in a Separate text file using python 2.7/3.

Bellow code just return The string between First Tag And does not consider the rest of the text.

with open('myfile.txt', 'r') as inF:
for num, line in enumerate(inF,1):
    if '</a>' in line:
        targetline = num+1
        f = open("myfile.txt")
        aa = ""
        for i in range(targetline):
            aa += f.next().strip() + "\n"
        f.close()
        fout = open("MyData1.txt", "w")
        finaltext = (aa.split('<a>'))[1].split('</a>')[0]
        fout.write(finaltext)
        fout.close()

Do you have any idea to do that ?

Using BeautifulSoup

Demo:

from bs4 import BeautifulSoup

with open(filename, 'r') as f, open(filename1, 'w') as outfile:
    soup = BeautifulSoup(f.read(), "html.parser")
    for i in soup.find_all("a"):
        print(i.text.strip())
        outfile.write(i.text.strip() + "\n")     #Write to new File

Output:

Some Text 1.....
Some Text 2.....
Some Text 3.....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM