Search code examples
pythonhtmlparsingbeautifulsouppre

Parse between pre tag Python


I'm trying to parse between PRE tags using Python using this code

s = br.open(base_url+str(string))
u = br.geturl()
seq = br.open(u)
blat = BeautifulSoup(seq)    
for res in blat.find('pre').findChildren():
        seq = res.string
        print seq

from the following HTML source code:

<PRE><TT>
<span style='color:#22CCEE;'>T</span><span style='color:#3300FF;'>AAAAGATGA</span> <span style='color:#3300FF;'>AGTTTCTATC</span> <span style='color:#3300FF;'>ATCCAAA</span>aa<span style='color:#3300FF;'>A</span> <span style='color:#3300FF;'>TGGGCTACAG</span> <span style='color:#3300FF;'>AAAC</span><span style='color:#22CCEE;'>C</span></TT></PRE>
<HR ALIGN="CENTER"><H4><A NAME=genomic></A>Genomic chr17 (reverse strand):</H4>
<PRE><TT>
tacatttttc tctaactgca aacataatgt tttcccttgt attttacaga  41256278
tgcaaacagc tataattttg caaaaaagga aaataactct cctgaacatc  41256228
<A NAME=1></A><span style='color:#22CCEE;'>T</span><span style='color:#3300FF;'>AAAAGATGA</span> <span style='color:#3300FF;'>AGTTTCTATC</span> <span style='color:#3300FF;'>ATCCAAA</span>gt<span style='color:#3300FF;'>A</span> <span style='color:#3300FF;'>TGGGCTACAG</span> <span style='color:#3300FF;'>AAAC</span><span style='color:#22CCEE;'>C</span>gtgcc  41256178
aaaagacttc tacagagtga acccgaaaat ccttccttgg taaaaccatt  41256128
tgttttcttc ttcttcttct tcttcttttc tttttttttt ctttt</TT></PRE>
<HR ALIGN="CENTER"><H4><A NAME=ali></A>Side by Side Alignment</H4>
<PRE><TT>
00000001 taaaagatgaagtttctatcatccaaaaaatgggctacagaaacc 00000045
<<<<<<<< |||||||||||||||||||||||||||  |||||||||||||||| <<<<<<<<
41256227 taaaagatgaagtttctatcatccaaagtatgggctacagaaacc 41256183

</TT></PRE>

It gives me the first PRE tag elements when I want to parse the last one. I'd appreciate any suggestions to achieve it. I'd like the output to be like:

00000001 taaaagatgaagtttctatcatccaaaaaatgggctacagaaacc 00000045
<<<<<<<< |||||||||||||||||||||||||||  |||||||||||||||| <<<<<<<<
41256227 taaaagatgaagtttctatcatccaaagtatgggctacagaaacc 41256183

whereas my current output is

T
AAAAGATGA
AGTTTCTATC
ATCCAAA
A
TGGGCTACAG
AAAC
C

Solution

  • You can use find_all() an get the last result:

    from bs4 import BeautifulSoup
    
    soup = BeautifulSoup(open('../index.html'), 'html5lib')
    
    pre = soup.find_all('pre')[-1]
    print pre.text.strip()
    

    where index.html contains the html you provided.

    It prints:

    00000001 taaaagatgaagtttctatcatccaaaaaatgggctacagaaacc 00000045
    <<<<<<<< ||||||||||||||||||||||||||| |||||||||||||||| <<<<<<<<
    41256227 taaaagatgaagtttctatcatccaaagtatgggctacagaaacc 41256183
    

    Another option would be to rely on the previous h4 tag to get the appropriate pre:

    h4 = soup.select('h4 > a[name="ali"]')[0].parent
    print h4.find_next_sibling('pre').text.strip()